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