Date.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\I18n;
  16. use Cake\Chronos\MutableDate;
  17. use IntlDateFormatter;
  18. use JsonSerializable;
  19. /**
  20. * Extends the Date class provided by Chronos.
  21. *
  22. * Adds handy methods and locale-aware formatting helpers
  23. */
  24. class Date extends MutableDate implements JsonSerializable
  25. {
  26. use DateFormatTrait;
  27. /**
  28. * The format to use when formatting a time using `Cake\I18n\Date::i18nFormat()`
  29. * and `__toString`
  30. *
  31. * The format should be either the formatting constants from IntlDateFormatter as
  32. * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
  33. * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
  34. *
  35. * It is possible to provide an array of 2 constants. In this case, the first position
  36. * will be used for formatting the date part of the object and the second position
  37. * will be used to format the time part.
  38. *
  39. * @var string|array|int
  40. * @see \Cake\I18n\DateFormatTrait::i18nFormat()
  41. */
  42. protected static $_toStringFormat = [IntlDateFormatter::SHORT, -1];
  43. /**
  44. * The format to use when formatting a time using `Cake\I18n\Date::timeAgoInWords()`
  45. * and the difference is more than `Cake\I18n\Date::$wordEnd`
  46. *
  47. * @var string
  48. * @see \Cake\I18n\DateFormatTrait::parseDate()
  49. */
  50. public static $wordFormat = [IntlDateFormatter::SHORT, -1];
  51. /**
  52. * The format to use when formatting a time using `Cake\I18n\Date::nice()`
  53. *
  54. * The format should be either the formatting constants from IntlDateFormatter as
  55. * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
  56. * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
  57. *
  58. * It is possible to provide an array of 2 constants. In this case, the first position
  59. * will be used for formatting the date part of the object and the second position
  60. * will be used to format the time part.
  61. *
  62. * @var string|array|int
  63. * @see \Cake\I18n\DateFormatTrait::nice()
  64. */
  65. public static $niceFormat = [IntlDateFormatter::MEDIUM, -1];
  66. /**
  67. * The format to use when formatting a time using `Date::timeAgoInWords()`
  68. * and the difference is less than `Date::$wordEnd`
  69. *
  70. * @var array
  71. * @see \Cake\I18n\Date::timeAgoInWords()
  72. */
  73. public static $wordAccuracy = [
  74. 'year' => "day",
  75. 'month' => "day",
  76. 'week' => "day",
  77. 'day' => "day",
  78. 'hour' => "day",
  79. 'minute' => "day",
  80. 'second' => "day",
  81. ];
  82. /**
  83. * The end of relative time telling
  84. *
  85. * @var string
  86. * @see \Cake\I18n\Date::timeAgoInWords()
  87. */
  88. public static $wordEnd = '+1 month';
  89. /**
  90. * Returns either a relative or a formatted absolute date depending
  91. * on the difference between the current date and this object.
  92. *
  93. * ### Options:
  94. *
  95. * - `from` => another Date object representing the "now" date
  96. * - `format` => a fall back format if the relative time is longer than the duration specified by end
  97. * - `accuracy` => Specifies how accurate the date should be described (array)
  98. * - year => The format if years > 0 (default "day")
  99. * - month => The format if months > 0 (default "day")
  100. * - week => The format if weeks > 0 (default "day")
  101. * - day => The format if weeks > 0 (default "day")
  102. * - `end` => The end of relative date telling
  103. * - `relativeString` => The printf compatible string when outputting relative date
  104. * - `absoluteString` => The printf compatible string when outputting absolute date
  105. * - `timezone` => The user timezone the timestamp should be formatted in.
  106. *
  107. * Relative dates look something like this:
  108. *
  109. * - 3 weeks, 4 days ago
  110. * - 1 day ago
  111. *
  112. * Default date formatting is d/M/YY e.g: on 18/2/09. Formatting is done internally using
  113. * `i18nFormat`, see the method for the valid formatting strings.
  114. *
  115. * The returned string includes 'ago' or 'on' and assumes you'll properly add a word
  116. * like 'Posted ' before the function output.
  117. *
  118. * NOTE: If the difference is one week or more, the lowest level of accuracy is day.
  119. *
  120. * @param array $options Array of options.
  121. * @return string Relative time string.
  122. */
  123. public function timeAgoInWords(array $options = [])
  124. {
  125. return $this->diffFormatter()->dateAgoInWords($this, $options);
  126. }
  127. }