forked from andre487/php_rutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDt.php
283 lines (240 loc) · 9.66 KB
/
Dt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace php_rutils;
use php_rutils\struct\TimeParams;
/**
* Russian dates without locales
* Class Dt
* @package php_rutils
*/
class Dt
{
const PREFIX_IN = "через"; //Day names (abbreviated, full, inflected, preposition)
const SUFFIX_AGO = "назад"; //Month names (abbreviated, full, inflected)
private static $_DAY_NAMES = array(
array('пн', 'понедельник', 'понедельник', "в\xa0"),
array('вт', 'вторник', 'вторник', "во\xa0"),
array('ср', 'среда', 'среду', "в\xa0"),
array('чт', 'четверг', 'четверг', "в\xa0"),
array('пт', 'пятница', 'пятницу', "в\xa0"),
array('сб', 'суббота', 'субботу', "в\xa0"),
array('вск', 'воскресенье', 'воскресенье', "в\xa0")
); //Day alternatives (i.e. one day ago -> yesterday)
private static $_MONTH_NAMES = array(
array("янв", "январь", "января"),
array("фев", "февраль", "февраля"),
array("мар", "март", "марта"),
array("апр", "апрель", "апреля"),
array("май", "май", "мая"),
array("июн", "июнь", "июня"),
array("июл", "июль", "июля"),
array("авг", "август", "августа"),
array("сен", "сентябрь", "сентября"),
array("окт", "октябрь", "октября"),
array("ноя", "ноябрь", "ноября"),
array("дек", "декабрь", "декабря"),
); //Forms (1, 2, 5) for noun 'day'
private static $_DAY_ALTERNATIVES = array(
1 => array("вчера", "завтра"),
2 => array("позавчера", "послезавтра"),
); //Forms (1, 2, 5) for noun 'hour'
private static $_DAY_VARIANTS = array("день", "дня", "дней"); //Forms (1, 2, 5) for noun 'minute'
private static $_HOUR_VARIANTS = array("час", "часа", "часов"); //Prefix 'in' (i.e. B{in} three hours)
private static $_MINUTE_VARIANTS = array("минуту", "минуты", "минут"); //Prefix 'ago' (i.e. three hours B{ago})
/**
* Russian \DateTime::format
* @param array|\php_rutils\struct\TimeParams $params Params structure
* @return string Date/time string representation
*/
public function ruStrFTime($params = null)
{
//Params handle
if ($params === null)
$params = new TimeParams();
elseif (is_array($params))
$params = TimeParams::create($params);
else
$params = clone $params;
if ($params->date === null) {
$params->date = new \DateTime();
}
elseif (is_numeric($params->date)) {
$timestamp = $params->date;
$params->date = new \DateTime();
$params->date->setTimestamp($timestamp);
}
elseif (is_string($params->date)) {
$params->date = new \DateTime($params->date);
}
if (is_string($params->timezone))
$params->timezone = new \DateTimeZone($params->timezone);
if ($params->timezone)
$params->date->setTimezone($params->timezone);
//Format processing
$weekday = $params->date->format('N') - 1;
$month = $params->date->format('n') - 1;
$prepos = $params->preposition ? self::$_DAY_NAMES[$weekday][3] : '';
$monthIdx = $params->monthInflected ? 2 : 1;
$dayIdx = ($params->dayInflected || $params->preposition) ? 2 : 1;
$search = array('D', 'l', 'M', 'F');
$replace = array(
$prepos.self::$_DAY_NAMES[$weekday][0],
$prepos.self::$_DAY_NAMES[$weekday][$dayIdx],
self::$_MONTH_NAMES[$month][0],
self::$_MONTH_NAMES[$month][$monthIdx],
);
//for russian typography standard,
//1 April 2007, but 01.04.2007
if (strpos($params->format, 'F') !== false || strpos($params->format, 'M') !== false) {
$search[] = 'd';
$replace[] = 'j';
}
$params->format = str_replace($search, $replace, $params->format);
//Create date/time string
return $params->date->format($params->format);
}
/**
* Represents distance of time in words
* @param string|int|\DateTime $toTime Source time
* @param string|int|\DateTime $fromTime Target time
* @param int $accuracy Level of accuracy (1..3), default=1
* @param string|\DateTimeZone|null $timeZone Time zone
* @throws \InvalidArgumentException
* @return string Distance of time in words
*/
public function distanceOfTimeInWords($toTime, $fromTime = null, $accuracy = 1, $timeZone = null)
{
if ($accuracy < 1 || $accuracy > 3)
throw new \InvalidArgumentException('Wrong accuracy value (must be 1..3)');
/* @var $toTime \DateTime */
/* @var $fromTime \DateTime */
/* @var $toCurrent bool */
list($toTime, $fromTime, $toCurrent) = $this->_createFunctionParams($toTime, $fromTime, $timeZone);
$interval = $toTime->diff($fromTime);
$words = array();
$values = array();
$alternatives = array();
$this->_fillCollections($interval, $toCurrent, $words, $values, $alternatives);
$this->_trimArrays($words);
$this->_trimArrays($values);
$limit = min($accuracy, sizeof($words));
$realWords = array_slice($words, 0, $limit);
$realValues = array_slice($values, 0, $limit);
$length0 = sizeof($realValues);
$this->_trimArrays($realValues, $realWords);
$limit -= ($length0 - sizeof($realValues));
//if diff less than one minute
if ($interval->i == 0 && !$realWords) {
if ($interval->invert)
$zeroStr = 'менее чем через минуту';
else
$zeroStr = 'менее минуты назад';
return $zeroStr;
}
//if diff is 1 or 2 days
$days = $interval->d;
if ($toCurrent && $limit == 1 && ($days == 1 || $days == 2)) {
$altData = self::$_DAY_ALTERNATIVES[$days];
$altDay = ($interval->invert) ? $altData[1] : $altData[0];
return $altDay;
}
//general case
$realStr = implode(', ', $realWords);
if ($limit == 1 && $toCurrent && $alternatives)
$altStr = $alternatives[0];
else
$altStr = '';
$resultStr = $altStr ? $altStr : $realStr;
if ($interval->invert)
$resultStr = self::PREFIX_IN.' '.$resultStr;
else
$resultStr = $resultStr.' '.self::SUFFIX_AGO;
return $resultStr;
}
/**
* Calculates age
* @param string|int|\DateTime $birthDate Date of birth
* @throws \InvalidArgumentException
* @return int Full years age
*/
public function getAge($birthDate)
{
if (!$birthDate) {
throw new \InvalidArgumentException('Wrong birth date');
}
if (is_numeric($birthDate)) {
$timestamp = $birthDate;
$birthDate = new \DateTime();
$birthDate->setTimestamp($timestamp);
}
else if (is_string($birthDate)) {
$birthDate = new \DateTime($birthDate);
}
$interval = $birthDate->diff(new \DateTime());
if ($interval->invert)
throw new \InvalidArgumentException('Wrong birth date');
return $interval->y;
}
private function _createFunctionParams($toTime, $fromTime, $timeZone)
{
if (is_numeric($toTime)) {
$timestamp = $toTime;
$toTime = new \DateTime();
$toTime->setTimestamp($timestamp);
}
else if (is_string($toTime)) {
$toTime = new \DateTime($toTime);
}
$toCurrent = false;
if ($fromTime === null) {
$fromTime = new \DateTime();
$toCurrent = true;
}
else if (is_numeric($fromTime)) {
$timestamp = $fromTime;
$fromTime = new \DateTime();
$fromTime->setTimestamp($timestamp);
}
else if (is_string($fromTime)) {
$fromTime = new \DateTime($fromTime);
}
if (is_string($timeZone))
$timeZone = new \DateTimeZone($timeZone);
if ($timeZone) {
$toTime->setTimezone($timeZone);
$fromTime->setTimezone($timeZone);
}
return array($toTime, $fromTime, $toCurrent);
}
private function _fillCollections($interval, $toCurrent, &$words, &$values, &$alternatives)
{
$numeral = new Numeral();
$days = $interval->days;
$words[] = $numeral->getPlural($days, self::$_DAY_VARIANTS);
$values[] = $days;
$hours = $interval->h;
$words[] = $numeral->getPlural($hours, self::$_HOUR_VARIANTS);
$values[] = $hours;
if ($days == 0 && $hours == 1 && $toCurrent)
$alternatives[] = 'час';
$minutes = $interval->i;
$words[] = $numeral->getPlural($minutes, self::$_MINUTE_VARIANTS);
$values[] = $minutes;
if ($days == 0 && $hours == 0 && $minutes == 1 && $toCurrent)
$alternatives[] = 'минуту';
}
private function _trimArrays(array &$arr, array &$arr2=null)
{
$i = sizeof($arr) - 1;
while ($i >= 0 && $arr[$i] == 0) {
array_pop($arr);
if ($arr2)
array_pop($arr2);
--$i;
}
while (sizeof($arr) && $arr[0] == 0) {
array_shift($arr);
if ($arr2)
array_shift($arr2);
}
}
}