1 | <?php |
||
9 | class Date { |
||
10 | private $locale; |
||
11 | |||
12 | public function __construct($locale) { |
||
15 | |||
16 | /** Converts $val into a \DateTime object if it's not already */ |
||
17 | private function getDate($val) { |
||
23 | |||
24 | /** Formats date based on supplied $format or default format from $locale */ |
||
25 | public function date($val, $format = null) { |
||
29 | |||
30 | /** Formats \DateTime as time based on supplied $format or default format from $locale */ |
||
31 | public function time($val, $format = null) { |
||
35 | |||
36 | /** Formats \DateTime as Date and Time using formats from $locale */ |
||
37 | public function dateTime($val) { |
||
40 | |||
41 | /** Generates relative time offsets based on system clock. e.g "10 minutes ago" or "In 6 months" |
||
42 | using strings from $locale */ |
||
43 | public function relative($val) { |
||
55 | |||
56 | /** Calculates offset in hours/minutes/seconds */ |
||
57 | private function timeOffset($diff) { |
||
75 | |||
76 | /** Gets date ranges to represent uses of weeks/months/days/etc */ |
||
77 | private function getRanges($strings) { |
||
78 | $ranges = [ |
||
79 | [1, 1, $strings['yesterday'], 1, ''], |
||
80 | [1, 13, $strings['past'], 1, 'days'], |
||
81 | [13, 28, $strings['past'], 7, 'weeks'], |
||
82 | [28, 365, $strings['past'], 28, 'months'], |
||
83 | [365, 99999999, $strings['past'], 365, 'years'], |
||
84 | [-1, -1, $strings['tomorrow'], 1, ''], |
||
85 | [-13, -1, $strings['future'], 1, 'days'], |
||
86 | [-28, -13, $strings['future'], 7, 'weeks'], |
||
87 | [-365, -28, $strings['future'], 28, 'months'], |
||
88 | [-999999, -365, $strings['future'], 365, 'years'], |
||
89 | ]; |
||
90 | if (isset($strings['day_before_yesterday'])) $this->add2DayRanges($ranges, $strings['day_before_yesterday'], "before"); |
||
91 | if (isset($strings['day_after_tomorrow'])) $this->add2DayRanges($ranges, $strings['day_after_tomorrow'], "after"); |
||
92 | return $ranges; |
||
93 | } |
||
94 | |||
95 | private function add2DayRanges(&$ranges, $property, $when) { |
||
96 | $rangeVal = $when === "before" ? 2 : -2; |
||
97 | array_unshift($ranges, [$rangeVal, $rangeVal, $property, 1, '']); |
||
98 | } |
||
99 | |||
100 | /** Converts "week" to "weeks", "month" to "months" etc when plural is required using language from $locale */ |
||
101 | private function getPlural($strings, $num, $interval) { |
||
105 | |||
106 | /** Calculates offset in days/weeks/month/years */ |
||
107 | private function dayOffset($diffDays) { |
||
122 | } |
||
123 |