TimeHelper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 0.10.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View\Helper;
  16. use Cake\I18n\Time;
  17. use Cake\View\Helper;
  18. use Cake\View\StringTemplateTrait;
  19. use Exception;
  20. /**
  21. * Time Helper class for easy use of time data.
  22. *
  23. * Manipulation of time data.
  24. *
  25. * @link https://book.cakephp.org/3.0/en/views/helpers/time.html
  26. * @see \Cake\I18n\Time
  27. */
  28. class TimeHelper extends Helper
  29. {
  30. use StringTemplateTrait;
  31. /**
  32. * Config options
  33. *
  34. * @var array
  35. */
  36. protected $_defaultConfig = [
  37. 'outputTimezone' => null
  38. ];
  39. /**
  40. * Get a timezone.
  41. *
  42. * Will use the provided timezone, or default output timezone if defined.
  43. *
  44. * @param null|string|\DateTimeZone $timezone The override timezone if applicable.
  45. * @return null|string|\DateTimeZone The chosen timezone or null.
  46. */
  47. protected function _getTimezone($timezone)
  48. {
  49. if ($timezone) {
  50. return $timezone;
  51. }
  52. return $this->getConfig('outputTimezone');
  53. }
  54. /**
  55. * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
  56. *
  57. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  58. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  59. * @return \Cake\I18n\Time
  60. */
  61. public function fromString($dateString, $timezone = null)
  62. {
  63. return (new Time($dateString))->timezone($timezone);
  64. }
  65. /**
  66. * Returns a nicely formatted date string for given Datetime string.
  67. *
  68. * @param int|string|\DateTime|null $dateString UNIX timestamp, strtotime() valid string or DateTime object
  69. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  70. * @param string|null $locale Locale string.
  71. * @return string Formatted date string
  72. */
  73. public function nice($dateString = null, $timezone = null, $locale = null)
  74. {
  75. $timezone = $this->_getTimezone($timezone);
  76. return (new Time($dateString))->nice($timezone, $locale);
  77. }
  78. /**
  79. * Returns true, if the given datetime string is today.
  80. *
  81. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  82. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  83. * @return bool True if the given datetime string is today.
  84. */
  85. public function isToday($dateString, $timezone = null)
  86. {
  87. return (new Time($dateString, $timezone))->isToday();
  88. }
  89. /**
  90. * Returns true, if the given datetime string is in the future.
  91. *
  92. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  93. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  94. * @return bool True if the given datetime string lies in the future.
  95. */
  96. public function isFuture($dateString, $timezone = null)
  97. {
  98. return (new Time($dateString, $timezone))->isFuture();
  99. }
  100. /**
  101. * Returns true, if the given datetime string is in the past.
  102. *
  103. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  104. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  105. * @return bool True if the given datetime string lies in the past.
  106. */
  107. public function isPast($dateString, $timezone = null)
  108. {
  109. return (new Time($dateString, $timezone))->isPast();
  110. }
  111. /**
  112. * Returns true if given datetime string is within this week.
  113. *
  114. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  115. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  116. * @return bool True if datetime string is within current week
  117. */
  118. public function isThisWeek($dateString, $timezone = null)
  119. {
  120. return (new Time($dateString, $timezone))->isThisWeek();
  121. }
  122. /**
  123. * Returns true if given datetime string is within this month
  124. *
  125. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  126. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  127. * @return bool True if datetime string is within the current month
  128. */
  129. public function isThisMonth($dateString, $timezone = null)
  130. {
  131. return (new Time($dateString, $timezone))->isThisMonth();
  132. }
  133. /**
  134. * Returns true if given datetime string is within the current year.
  135. *
  136. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  137. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  138. * @return bool True if datetime string is within current year
  139. */
  140. public function isThisYear($dateString, $timezone = null)
  141. {
  142. return (new Time($dateString, $timezone))->isThisYear();
  143. }
  144. /**
  145. * Returns true if given datetime string was yesterday.
  146. *
  147. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  148. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  149. * @return bool True if datetime string was yesterday
  150. *
  151. */
  152. public function wasYesterday($dateString, $timezone = null)
  153. {
  154. return (new Time($dateString, $timezone))->isYesterday();
  155. }
  156. /**
  157. * Returns true if given datetime string is tomorrow.
  158. *
  159. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  160. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  161. * @return bool True if datetime string was yesterday
  162. */
  163. public function isTomorrow($dateString, $timezone = null)
  164. {
  165. return (new Time($dateString, $timezone))->isTomorrow();
  166. }
  167. /**
  168. * Returns the quarter
  169. *
  170. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  171. * @param bool $range if true returns a range in Y-m-d format
  172. * @return int|array 1, 2, 3, or 4 quarter of year or array if $range true
  173. * @see \Cake\I18n\Time::toQuarter()
  174. */
  175. public function toQuarter($dateString, $range = false)
  176. {
  177. return (new Time($dateString))->toQuarter($range);
  178. }
  179. /**
  180. * Returns a UNIX timestamp from a textual datetime description.
  181. *
  182. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  183. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  184. * @return string UNIX timestamp
  185. * @see \Cake\I18n\Time::toUnix()
  186. */
  187. public function toUnix($dateString, $timezone = null)
  188. {
  189. return (new Time($dateString, $timezone))->toUnixString();
  190. }
  191. /**
  192. * Returns a date formatted for Atom RSS feeds.
  193. *
  194. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  195. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  196. * @return string Formatted date string
  197. * @see \Cake\I18n\Time::toAtom()
  198. */
  199. public function toAtom($dateString, $timezone = null)
  200. {
  201. $timezone = $this->_getTimezone($timezone) ?: date_default_timezone_get();
  202. return (new Time($dateString))->timezone($timezone)->toAtomString();
  203. }
  204. /**
  205. * Formats date for RSS feeds
  206. *
  207. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  208. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  209. * @return string Formatted date string
  210. */
  211. public function toRss($dateString, $timezone = null)
  212. {
  213. $timezone = $this->_getTimezone($timezone) ?: date_default_timezone_get();
  214. return (new Time($dateString))->timezone($timezone)->toRssString();
  215. }
  216. /**
  217. * Formats a date into a phrase expressing the relative time.
  218. *
  219. * ### Additional options
  220. *
  221. * - `element` - The element to wrap the formatted time in.
  222. * Has a few additional options:
  223. * - `tag` - The tag to use, defaults to 'span'.
  224. * - `class` - The class name to use, defaults to `time-ago-in-words`.
  225. * - `title` - Defaults to the $dateTime input.
  226. *
  227. * @param int|string|\DateTime|\Cake\Chronos\ChronosInterface $dateTime UNIX timestamp, strtotime() valid string or DateTime object
  228. * @param array $options Default format if timestamp is used in $dateString
  229. * @return string Relative time string.
  230. * @see \Cake\I18n\Time::timeAgoInWords()
  231. */
  232. public function timeAgoInWords($dateTime, array $options = [])
  233. {
  234. $element = null;
  235. $options += [
  236. 'element' => null,
  237. 'timezone' => null
  238. ];
  239. $options['timezone'] = $this->_getTimezone($options['timezone']);
  240. if ($options['timezone']) {
  241. $dateTime = $dateTime->timezone($options['timezone']);
  242. unset($options['timezone']);
  243. }
  244. if (!empty($options['element'])) {
  245. $element = [
  246. 'tag' => 'span',
  247. 'class' => 'time-ago-in-words',
  248. 'title' => $dateTime
  249. ];
  250. if (is_array($options['element'])) {
  251. $element = $options['element'] + $element;
  252. } else {
  253. $element['tag'] = $options['element'];
  254. }
  255. unset($options['element']);
  256. }
  257. $relativeDate = (new Time($dateTime))->timeAgoInWords($options);
  258. if ($element) {
  259. $relativeDate = sprintf(
  260. '<%s%s>%s</%s>',
  261. $element['tag'],
  262. $this->templater()->formatAttributes($element, ['tag']),
  263. $relativeDate,
  264. $element['tag']
  265. );
  266. }
  267. return $relativeDate;
  268. }
  269. /**
  270. * Returns true if specified datetime was within the interval specified, else false.
  271. *
  272. * @param string|int $timeInterval the numeric value with space then time type.
  273. * Example of valid types: 6 hours, 2 days, 1 minute.
  274. * Integer input values are deprecated and support will be removed in 4.0.0
  275. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  276. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  277. * @return bool
  278. * @see \Cake\I18n\Time::wasWithinLast()
  279. */
  280. public function wasWithinLast($timeInterval, $dateString, $timezone = null)
  281. {
  282. return (new Time($dateString, $timezone))->wasWithinLast($timeInterval);
  283. }
  284. /**
  285. * Returns true if specified datetime is within the interval specified, else false.
  286. *
  287. * @param string|int $timeInterval the numeric value with space then time type.
  288. * Example of valid types: 6 hours, 2 days, 1 minute.
  289. * Integer input values are deprecated and support will be removed in 4.0.0
  290. * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  291. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  292. * @return bool
  293. * @see \Cake\I18n\Time::wasWithinLast()
  294. */
  295. public function isWithinNext($timeInterval, $dateString, $timezone = null)
  296. {
  297. return (new Time($dateString, $timezone))->isWithinNext($timeInterval);
  298. }
  299. /**
  300. * Returns gmt as a UNIX timestamp.
  301. *
  302. * @param int|string|\DateTime|null $string UNIX timestamp, strtotime() valid string or DateTime object
  303. * @return string UNIX timestamp
  304. * @see \Cake\I18n\Time::gmt()
  305. */
  306. public function gmt($string = null)
  307. {
  308. return (new Time($string))->toUnixString();
  309. }
  310. /**
  311. * Returns a formatted date string, given either a Time instance,
  312. * UNIX timestamp or a valid strtotime() date string.
  313. *
  314. * This method is an alias for TimeHelper::i18nFormat().
  315. *
  316. * @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
  317. * @param int|string|null $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
  318. * @param bool|string $invalid Default value to display on invalid dates
  319. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  320. * @return string Formatted and translated date string
  321. * @see \Cake\I18n\Time::i18nFormat()
  322. */
  323. public function format($date, $format = null, $invalid = false, $timezone = null)
  324. {
  325. return $this->i18nFormat($date, $format, $invalid, $timezone);
  326. }
  327. /**
  328. * Returns a formatted date string, given either a Datetime instance,
  329. * UNIX timestamp or a valid strtotime() date string.
  330. *
  331. * @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
  332. * @param string|null $format Intl compatible format string.
  333. * @param bool|string $invalid Default value to display on invalid dates
  334. * @param string|\DateTimeZone|null $timezone User's timezone string or DateTimeZone object
  335. * @return string Formatted and translated date string
  336. * @throws \Exception When the date cannot be parsed
  337. * @see \Cake\I18n\Time::i18nFormat()
  338. */
  339. public function i18nFormat($date, $format = null, $invalid = false, $timezone = null)
  340. {
  341. if (!isset($date)) {
  342. return $invalid;
  343. }
  344. $timezone = $this->_getTimezone($timezone);
  345. try {
  346. $time = new Time($date);
  347. return $time->i18nFormat($format, $timezone);
  348. } catch (Exception $e) {
  349. if ($invalid === false) {
  350. throw $e;
  351. }
  352. return $invalid;
  353. }
  354. }
  355. /**
  356. * Event listeners.
  357. *
  358. * @return array
  359. */
  360. public function implementedEvents()
  361. {
  362. return [];
  363. }
  364. }