3
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 * This is a concatenated copy of the following files:
|
|
5 * Horde/Date/Utils.php, Horde/Date/Recurrence.php
|
|
6 * Pull the latest version of these files from the PEAR channel of the Horde
|
|
7 * project at http://pear.horde.org by installing the Horde_Date package.
|
|
8 */
|
|
9
|
|
10
|
|
11 /**
|
|
12 * Horde Date wrapper/logic class, including some calculation
|
|
13 * functions.
|
|
14 *
|
|
15 * @category Horde
|
|
16 * @package Date
|
|
17 *
|
|
18 * @TODO in format():
|
|
19 * http://php.net/intldateformatter
|
|
20 *
|
|
21 * @TODO on timezones:
|
|
22 * http://trac.agavi.org/ticket/1008
|
|
23 * http://trac.agavi.org/changeset/3659
|
|
24 *
|
|
25 * @TODO on switching to PHP::DateTime:
|
|
26 * The only thing ever stored in the database *IS* Unix timestamps. Doing
|
|
27 * anything other than that is unmanageable, yet some frameworks use 'server
|
|
28 * based' times in their systems, simply because they do not bother with
|
|
29 * daylight saving and only 'serve' one timezone!
|
|
30 *
|
|
31 * The second you have to manage 'real' time across timezones then daylight
|
|
32 * saving becomes essential, BUT only on the display side! Since the browser
|
|
33 * only provides a time offset, this is useless and to be honest should simply
|
|
34 * be ignored ( until it is upgraded to provide the correct information ;)
|
|
35 * ). So we need a 'display' function that takes a simple numeric epoch, and a
|
|
36 * separate timezone id into which the epoch is to be 'converted'. My W3C
|
|
37 * mapping works simply because ADOdb then converts that to it's own simple
|
|
38 * offset abbreviation - in my case GMT or BST. As long as DateTime passes the
|
|
39 * full 64 bit number the date range from 100AD is also preserved ( and
|
|
40 * further back if 2 digit years are disabled ). If I want to display the
|
|
41 * 'real' timezone with this 'time' then I just add it in place of ADOdb's
|
|
42 * 'timezone'. I am tempted to simply adjust the ADOdb class to take a
|
|
43 * timezone in place of the simple GMT switch it currently uses.
|
|
44 *
|
|
45 * The return path is just the reverse and simply needs to take the client
|
|
46 * display offset off prior to storage of the UTC epoch. SO we use
|
|
47 * DateTimeZone to get an offset value for the clients timezone and simply add
|
|
48 * or subtract this from a timezone agnostic display on the client end when
|
|
49 * entering new times.
|
|
50 *
|
|
51 *
|
|
52 * It's not really feasible to store dates in specific timezone, as most
|
|
53 * national/local timezones support DST - and that is a pain to support, as
|
|
54 * eg. sorting breaks when some timestamps get repeated. That's why it's
|
|
55 * usually better to store datetimes as either UTC datetime or plain unix
|
|
56 * timestamp. I usually go with the former - using database datetime type.
|
|
57 */
|
|
58
|
|
59 /**
|
|
60 * @category Horde
|
|
61 * @package Date
|
|
62 */
|
|
63 class Horde_Date
|
|
64 {
|
|
65 const DATE_SUNDAY = 0;
|
|
66 const DATE_MONDAY = 1;
|
|
67 const DATE_TUESDAY = 2;
|
|
68 const DATE_WEDNESDAY = 3;
|
|
69 const DATE_THURSDAY = 4;
|
|
70 const DATE_FRIDAY = 5;
|
|
71 const DATE_SATURDAY = 6;
|
|
72
|
|
73 const MASK_SUNDAY = 1;
|
|
74 const MASK_MONDAY = 2;
|
|
75 const MASK_TUESDAY = 4;
|
|
76 const MASK_WEDNESDAY = 8;
|
|
77 const MASK_THURSDAY = 16;
|
|
78 const MASK_FRIDAY = 32;
|
|
79 const MASK_SATURDAY = 64;
|
|
80 const MASK_WEEKDAYS = 62;
|
|
81 const MASK_WEEKEND = 65;
|
|
82 const MASK_ALLDAYS = 127;
|
|
83
|
|
84 const MASK_SECOND = 1;
|
|
85 const MASK_MINUTE = 2;
|
|
86 const MASK_HOUR = 4;
|
|
87 const MASK_DAY = 8;
|
|
88 const MASK_MONTH = 16;
|
|
89 const MASK_YEAR = 32;
|
|
90 const MASK_ALLPARTS = 63;
|
|
91
|
|
92 const DATE_DEFAULT = 'Y-m-d H:i:s';
|
|
93 const DATE_JSON = 'Y-m-d\TH:i:s';
|
|
94
|
|
95 /**
|
|
96 * Year
|
|
97 *
|
|
98 * @var integer
|
|
99 */
|
|
100 protected $_year;
|
|
101
|
|
102 /**
|
|
103 * Month
|
|
104 *
|
|
105 * @var integer
|
|
106 */
|
|
107 protected $_month;
|
|
108
|
|
109 /**
|
|
110 * Day
|
|
111 *
|
|
112 * @var integer
|
|
113 */
|
|
114 protected $_mday;
|
|
115
|
|
116 /**
|
|
117 * Hour
|
|
118 *
|
|
119 * @var integer
|
|
120 */
|
|
121 protected $_hour = 0;
|
|
122
|
|
123 /**
|
|
124 * Minute
|
|
125 *
|
|
126 * @var integer
|
|
127 */
|
|
128 protected $_min = 0;
|
|
129
|
|
130 /**
|
|
131 * Second
|
|
132 *
|
|
133 * @var integer
|
|
134 */
|
|
135 protected $_sec = 0;
|
|
136
|
|
137 /**
|
|
138 * String representation of the date's timezone.
|
|
139 *
|
|
140 * @var string
|
|
141 */
|
|
142 protected $_timezone;
|
|
143
|
|
144 /**
|
|
145 * Default format for __toString()
|
|
146 *
|
|
147 * @var string
|
|
148 */
|
|
149 protected $_defaultFormat = self::DATE_DEFAULT;
|
|
150
|
|
151 /**
|
|
152 * Default specs that are always supported.
|
|
153 * @var string
|
|
154 */
|
|
155 protected static $_defaultSpecs = '%CdDeHImMnRStTyY';
|
|
156
|
|
157 /**
|
|
158 * Internally supported strftime() specifiers.
|
|
159 * @var string
|
|
160 */
|
|
161 protected static $_supportedSpecs = '';
|
|
162
|
|
163 /**
|
|
164 * Map of required correction masks.
|
|
165 *
|
|
166 * @see __set()
|
|
167 *
|
|
168 * @var array
|
|
169 */
|
|
170 protected static $_corrections = array(
|
|
171 'year' => self::MASK_YEAR,
|
|
172 'month' => self::MASK_MONTH,
|
|
173 'mday' => self::MASK_DAY,
|
|
174 'hour' => self::MASK_HOUR,
|
|
175 'min' => self::MASK_MINUTE,
|
|
176 'sec' => self::MASK_SECOND,
|
|
177 );
|
|
178
|
|
179 protected $_formatCache = array();
|
|
180
|
|
181 /**
|
|
182 * Builds a new date object. If $date contains date parts, use them to
|
|
183 * initialize the object.
|
|
184 *
|
|
185 * Recognized formats:
|
|
186 * - arrays with keys 'year', 'month', 'mday', 'day'
|
|
187 * 'hour', 'min', 'minute', 'sec'
|
|
188 * - objects with properties 'year', 'month', 'mday', 'hour', 'min', 'sec'
|
|
189 * - yyyy-mm-dd hh:mm:ss
|
|
190 * - yyyymmddhhmmss
|
|
191 * - yyyymmddThhmmssZ
|
|
192 * - yyyymmdd (might conflict with unix timestamps between 31 Oct 1966 and
|
|
193 * 03 Mar 1973)
|
|
194 * - unix timestamps
|
|
195 * - anything parsed by strtotime()/DateTime.
|
|
196 *
|
|
197 * @throws Horde_Date_Exception
|
|
198 */
|
|
199 public function __construct($date = null, $timezone = null)
|
|
200 {
|
|
201 if (!self::$_supportedSpecs) {
|
|
202 self::$_supportedSpecs = self::$_defaultSpecs;
|
|
203 if (function_exists('nl_langinfo')) {
|
|
204 self::$_supportedSpecs .= 'bBpxX';
|
|
205 }
|
|
206 }
|
|
207
|
|
208 if (func_num_args() > 2) {
|
|
209 // Handle args in order: year month day hour min sec tz
|
|
210 $this->_initializeFromArgs(func_get_args());
|
|
211 return;
|
|
212 }
|
|
213
|
|
214 $this->_initializeTimezone($timezone);
|
|
215
|
|
216 if (is_null($date)) {
|
|
217 return;
|
|
218 }
|
|
219
|
|
220 if (is_string($date)) {
|
|
221 $date = trim($date, '"');
|
|
222 }
|
|
223
|
|
224 if (is_object($date)) {
|
|
225 $this->_initializeFromObject($date);
|
|
226 } elseif (is_array($date)) {
|
|
227 $this->_initializeFromArray($date);
|
|
228 } elseif (preg_match('/^(\d{4})-?(\d{2})-?(\d{2})T? ?(\d{2}):?(\d{2}):?(\d{2})(?:\.\d+)?(Z?)$/', $date, $parts)) {
|
|
229 $this->_year = (int)$parts[1];
|
|
230 $this->_month = (int)$parts[2];
|
|
231 $this->_mday = (int)$parts[3];
|
|
232 $this->_hour = (int)$parts[4];
|
|
233 $this->_min = (int)$parts[5];
|
|
234 $this->_sec = (int)$parts[6];
|
|
235 if ($parts[7]) {
|
|
236 $this->_initializeTimezone('UTC');
|
|
237 }
|
|
238 } elseif (preg_match('/^(\d{4})-?(\d{2})-?(\d{2})$/', $date, $parts) &&
|
|
239 $parts[2] > 0 && $parts[2] <= 12 &&
|
|
240 $parts[3] > 0 && $parts[3] <= 31) {
|
|
241 $this->_year = (int)$parts[1];
|
|
242 $this->_month = (int)$parts[2];
|
|
243 $this->_mday = (int)$parts[3];
|
|
244 $this->_hour = $this->_min = $this->_sec = 0;
|
|
245 } elseif ((string)(int)$date == $date) {
|
|
246 // Try as a timestamp.
|
|
247 $parts = @getdate($date);
|
|
248 if ($parts) {
|
|
249 $this->_year = $parts['year'];
|
|
250 $this->_month = $parts['mon'];
|
|
251 $this->_mday = $parts['mday'];
|
|
252 $this->_hour = $parts['hours'];
|
|
253 $this->_min = $parts['minutes'];
|
|
254 $this->_sec = $parts['seconds'];
|
|
255 }
|
|
256 } else {
|
|
257 // Use date_create() so we can catch errors with PHP 5.2. Use
|
|
258 // "new DateTime() once we require 5.3.
|
|
259 $parsed = date_create($date);
|
|
260 if (!$parsed) {
|
|
261 throw new Horde_Date_Exception(sprintf(Horde_Date_Translation::t("Failed to parse time string (%s)"), $date));
|
|
262 }
|
|
263 $parsed->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
264 $this->_year = (int)$parsed->format('Y');
|
|
265 $this->_month = (int)$parsed->format('m');
|
|
266 $this->_mday = (int)$parsed->format('d');
|
|
267 $this->_hour = (int)$parsed->format('H');
|
|
268 $this->_min = (int)$parsed->format('i');
|
|
269 $this->_sec = (int)$parsed->format('s');
|
|
270 $this->_initializeTimezone(date_default_timezone_get());
|
|
271 }
|
|
272 }
|
|
273
|
|
274 /**
|
|
275 * Returns a simple string representation of the date object
|
|
276 *
|
|
277 * @return string This object converted to a string.
|
|
278 */
|
|
279 public function __toString()
|
|
280 {
|
|
281 try {
|
|
282 return $this->format($this->_defaultFormat);
|
|
283 } catch (Exception $e) {
|
|
284 return '';
|
|
285 }
|
|
286 }
|
|
287
|
|
288 /**
|
|
289 * Returns a DateTime object representing this object.
|
|
290 *
|
|
291 * @return DateTime
|
|
292 */
|
|
293 public function toDateTime()
|
|
294 {
|
|
295 $date = new DateTime(null, new DateTimeZone($this->_timezone));
|
|
296 $date->setDate($this->_year, $this->_month, $this->_mday);
|
|
297 $date->setTime($this->_hour, $this->_min, $this->_sec);
|
|
298 return $date;
|
|
299 }
|
|
300
|
|
301 /**
|
|
302 * Converts a date in the proleptic Gregorian calendar to the no of days
|
|
303 * since 24th November, 4714 B.C.
|
|
304 *
|
|
305 * Returns the no of days since Monday, 24th November, 4714 B.C. in the
|
|
306 * proleptic Gregorian calendar (which is 24th November, -4713 using
|
|
307 * 'Astronomical' year numbering, and 1st January, 4713 B.C. in the
|
|
308 * proleptic Julian calendar). This is also the first day of the 'Julian
|
|
309 * Period' proposed by Joseph Scaliger in 1583, and the number of days
|
|
310 * since this date is known as the 'Julian Day'. (It is not directly
|
|
311 * to do with the Julian calendar, although this is where the name
|
|
312 * is derived from.)
|
|
313 *
|
|
314 * The algorithm is valid for all years (positive and negative), and
|
|
315 * also for years preceding 4714 B.C.
|
|
316 *
|
|
317 * Algorithm is from PEAR::Date_Calc
|
|
318 *
|
|
319 * @author Monte Ohrt <monte@ispi.net>
|
|
320 * @author Pierre-Alain Joye <pajoye@php.net>
|
|
321 * @author Daniel Convissor <danielc@php.net>
|
|
322 * @author C.A. Woodcock <c01234@netcomuk.co.uk>
|
|
323 *
|
|
324 * @return integer The number of days since 24th November, 4714 B.C.
|
|
325 */
|
|
326 public function toDays()
|
|
327 {
|
|
328 if (function_exists('GregorianToJD')) {
|
|
329 return gregoriantojd($this->_month, $this->_mday, $this->_year);
|
|
330 }
|
|
331
|
|
332 $day = $this->_mday;
|
|
333 $month = $this->_month;
|
|
334 $year = $this->_year;
|
|
335
|
|
336 if ($month > 2) {
|
|
337 // March = 0, April = 1, ..., December = 9,
|
|
338 // January = 10, February = 11
|
|
339 $month -= 3;
|
|
340 } else {
|
|
341 $month += 9;
|
|
342 --$year;
|
|
343 }
|
|
344
|
|
345 $hb_negativeyear = $year < 0;
|
|
346 $century = intval($year / 100);
|
|
347 $year = $year % 100;
|
|
348
|
|
349 if ($hb_negativeyear) {
|
|
350 // Subtract 1 because year 0 is a leap year;
|
|
351 // And N.B. that we must treat the leap years as occurring
|
|
352 // one year earlier than they do, because for the purposes
|
|
353 // of calculation, the year starts on 1st March:
|
|
354 //
|
|
355 return intval((14609700 * $century + ($year == 0 ? 1 : 0)) / 400) +
|
|
356 intval((1461 * $year + 1) / 4) +
|
|
357 intval((153 * $month + 2) / 5) +
|
|
358 $day + 1721118;
|
|
359 } else {
|
|
360 return intval(146097 * $century / 4) +
|
|
361 intval(1461 * $year / 4) +
|
|
362 intval((153 * $month + 2) / 5) +
|
|
363 $day + 1721119;
|
|
364 }
|
|
365 }
|
|
366
|
|
367 /**
|
|
368 * Converts number of days since 24th November, 4714 B.C. (in the proleptic
|
|
369 * Gregorian calendar, which is year -4713 using 'Astronomical' year
|
|
370 * numbering) to Gregorian calendar date.
|
|
371 *
|
|
372 * Returned date belongs to the proleptic Gregorian calendar, using
|
|
373 * 'Astronomical' year numbering.
|
|
374 *
|
|
375 * The algorithm is valid for all years (positive and negative), and
|
|
376 * also for years preceding 4714 B.C. (i.e. for negative 'Julian Days'),
|
|
377 * and so the only limitation is platform-dependent (for 32-bit systems
|
|
378 * the maximum year would be something like about 1,465,190 A.D.).
|
|
379 *
|
|
380 * N.B. Monday, 24th November, 4714 B.C. is Julian Day '0'.
|
|
381 *
|
|
382 * Algorithm is from PEAR::Date_Calc
|
|
383 *
|
|
384 * @author Monte Ohrt <monte@ispi.net>
|
|
385 * @author Pierre-Alain Joye <pajoye@php.net>
|
|
386 * @author Daniel Convissor <danielc@php.net>
|
|
387 * @author C.A. Woodcock <c01234@netcomuk.co.uk>
|
|
388 *
|
|
389 * @param int $days the number of days since 24th November, 4714 B.C.
|
|
390 * @param string $format the string indicating how to format the output
|
|
391 *
|
|
392 * @return Horde_Date A Horde_Date object representing the date.
|
|
393 */
|
|
394 public static function fromDays($days)
|
|
395 {
|
|
396 if (function_exists('JDToGregorian')) {
|
|
397 list($month, $day, $year) = explode('/', JDToGregorian($days));
|
|
398 } else {
|
|
399 $days = intval($days);
|
|
400
|
|
401 $days -= 1721119;
|
|
402 $century = floor((4 * $days - 1) / 146097);
|
|
403 $days = floor(4 * $days - 1 - 146097 * $century);
|
|
404 $day = floor($days / 4);
|
|
405
|
|
406 $year = floor((4 * $day + 3) / 1461);
|
|
407 $day = floor(4 * $day + 3 - 1461 * $year);
|
|
408 $day = floor(($day + 4) / 4);
|
|
409
|
|
410 $month = floor((5 * $day - 3) / 153);
|
|
411 $day = floor(5 * $day - 3 - 153 * $month);
|
|
412 $day = floor(($day + 5) / 5);
|
|
413
|
|
414 $year = $century * 100 + $year;
|
|
415 if ($month < 10) {
|
|
416 $month +=3;
|
|
417 } else {
|
|
418 $month -=9;
|
|
419 ++$year;
|
|
420 }
|
|
421 }
|
|
422
|
|
423 return new Horde_Date($year, $month, $day);
|
|
424 }
|
|
425
|
|
426 /**
|
|
427 * Getter for the date and time properties.
|
|
428 *
|
|
429 * @param string $name One of 'year', 'month', 'mday', 'hour', 'min' or
|
|
430 * 'sec'.
|
|
431 *
|
|
432 * @return integer The property value, or null if not set.
|
|
433 */
|
|
434 public function __get($name)
|
|
435 {
|
|
436 if ($name == 'day') {
|
|
437 $name = 'mday';
|
|
438 }
|
|
439
|
|
440 return $this->{'_' . $name};
|
|
441 }
|
|
442
|
|
443 /**
|
|
444 * Setter for the date and time properties.
|
|
445 *
|
|
446 * @param string $name One of 'year', 'month', 'mday', 'hour', 'min' or
|
|
447 * 'sec'.
|
|
448 * @param integer $value The property value.
|
|
449 */
|
|
450 public function __set($name, $value)
|
|
451 {
|
|
452 if ($name == 'timezone') {
|
|
453 $this->_initializeTimezone($value);
|
|
454 return;
|
|
455 }
|
|
456 if ($name == 'day') {
|
|
457 $name = 'mday';
|
|
458 }
|
|
459
|
|
460 if ($name != 'year' && $name != 'month' && $name != 'mday' &&
|
|
461 $name != 'hour' && $name != 'min' && $name != 'sec') {
|
|
462 throw new InvalidArgumentException('Undefined property ' . $name);
|
|
463 }
|
|
464
|
|
465 $down = $value < $this->{'_' . $name};
|
|
466 $this->{'_' . $name} = $value;
|
|
467 $this->_correct(self::$_corrections[$name], $down);
|
|
468 $this->_formatCache = array();
|
|
469 }
|
|
470
|
|
471 /**
|
|
472 * Returns whether a date or time property exists.
|
|
473 *
|
|
474 * @param string $name One of 'year', 'month', 'mday', 'hour', 'min' or
|
|
475 * 'sec'.
|
|
476 *
|
|
477 * @return boolen True if the property exists and is set.
|
|
478 */
|
|
479 public function __isset($name)
|
|
480 {
|
|
481 if ($name == 'day') {
|
|
482 $name = 'mday';
|
|
483 }
|
|
484 return ($name == 'year' || $name == 'month' || $name == 'mday' ||
|
|
485 $name == 'hour' || $name == 'min' || $name == 'sec') &&
|
|
486 isset($this->{'_' . $name});
|
|
487 }
|
|
488
|
|
489 /**
|
|
490 * Adds a number of seconds or units to this date, returning a new Date
|
|
491 * object.
|
|
492 */
|
|
493 public function add($factor)
|
|
494 {
|
|
495 $d = clone($this);
|
|
496 if (is_array($factor) || is_object($factor)) {
|
|
497 foreach ($factor as $property => $value) {
|
|
498 $d->$property += $value;
|
|
499 }
|
|
500 } else {
|
|
501 $d->sec += $factor;
|
|
502 }
|
|
503
|
|
504 return $d;
|
|
505 }
|
|
506
|
|
507 /**
|
|
508 * Subtracts a number of seconds or units from this date, returning a new
|
|
509 * Horde_Date object.
|
|
510 */
|
|
511 public function sub($factor)
|
|
512 {
|
|
513 if (is_array($factor)) {
|
|
514 foreach ($factor as &$value) {
|
|
515 $value *= -1;
|
|
516 }
|
|
517 } else {
|
|
518 $factor *= -1;
|
|
519 }
|
|
520
|
|
521 return $this->add($factor);
|
|
522 }
|
|
523
|
|
524 /**
|
|
525 * Converts this object to a different timezone.
|
|
526 *
|
|
527 * @param string $timezone The new timezone.
|
|
528 *
|
|
529 * @return Horde_Date This object.
|
|
530 */
|
|
531 public function setTimezone($timezone)
|
|
532 {
|
|
533 $date = $this->toDateTime();
|
|
534 $date->setTimezone(new DateTimeZone($timezone));
|
|
535 $this->_timezone = $timezone;
|
|
536 $this->_year = (int)$date->format('Y');
|
|
537 $this->_month = (int)$date->format('m');
|
|
538 $this->_mday = (int)$date->format('d');
|
|
539 $this->_hour = (int)$date->format('H');
|
|
540 $this->_min = (int)$date->format('i');
|
|
541 $this->_sec = (int)$date->format('s');
|
|
542 $this->_formatCache = array();
|
|
543 return $this;
|
|
544 }
|
|
545
|
|
546 /**
|
|
547 * Sets the default date format used in __toString()
|
|
548 *
|
|
549 * @param string $format
|
|
550 */
|
|
551 public function setDefaultFormat($format)
|
|
552 {
|
|
553 $this->_defaultFormat = $format;
|
|
554 }
|
|
555
|
|
556 /**
|
|
557 * Returns the day of the week (0 = Sunday, 6 = Saturday) of this date.
|
|
558 *
|
|
559 * @return integer The day of the week.
|
|
560 */
|
|
561 public function dayOfWeek()
|
|
562 {
|
|
563 if ($this->_month > 2) {
|
|
564 $month = $this->_month - 2;
|
|
565 $year = $this->_year;
|
|
566 } else {
|
|
567 $month = $this->_month + 10;
|
|
568 $year = $this->_year - 1;
|
|
569 }
|
|
570
|
|
571 $day = (floor((13 * $month - 1) / 5) +
|
|
572 $this->_mday + ($year % 100) +
|
|
573 floor(($year % 100) / 4) +
|
|
574 floor(($year / 100) / 4) - 2 *
|
|
575 floor($year / 100) + 77);
|
|
576
|
|
577 return (int)($day - 7 * floor($day / 7));
|
|
578 }
|
|
579
|
|
580 /**
|
|
581 * Returns the day number of the year (1 to 365/366).
|
|
582 *
|
|
583 * @return integer The day of the year.
|
|
584 */
|
|
585 public function dayOfYear()
|
|
586 {
|
|
587 return $this->format('z') + 1;
|
|
588 }
|
|
589
|
|
590 /**
|
|
591 * Returns the week of the month.
|
|
592 *
|
|
593 * @return integer The week number.
|
|
594 */
|
|
595 public function weekOfMonth()
|
|
596 {
|
|
597 return ceil($this->_mday / 7);
|
|
598 }
|
|
599
|
|
600 /**
|
|
601 * Returns the week of the year, first Monday is first day of first week.
|
|
602 *
|
|
603 * @return integer The week number.
|
|
604 */
|
|
605 public function weekOfYear()
|
|
606 {
|
|
607 return $this->format('W');
|
|
608 }
|
|
609
|
|
610 /**
|
|
611 * Returns the number of weeks in the given year (52 or 53).
|
|
612 *
|
|
613 * @param integer $year The year to count the number of weeks in.
|
|
614 *
|
|
615 * @return integer $numWeeks The number of weeks in $year.
|
|
616 */
|
|
617 public static function weeksInYear($year)
|
|
618 {
|
|
619 // Find the last Thursday of the year.
|
|
620 $date = new Horde_Date($year . '-12-31');
|
|
621 while ($date->dayOfWeek() != self::DATE_THURSDAY) {
|
|
622 --$date->mday;
|
|
623 }
|
|
624 return $date->weekOfYear();
|
|
625 }
|
|
626
|
|
627 /**
|
|
628 * Sets the date of this object to the $nth weekday of $weekday.
|
|
629 *
|
|
630 * @param integer $weekday The day of the week (0 = Sunday, etc).
|
|
631 * @param integer $nth The $nth $weekday to set to (defaults to 1).
|
|
632 */
|
|
633 public function setNthWeekday($weekday, $nth = 1)
|
|
634 {
|
|
635 if ($weekday < self::DATE_SUNDAY || $weekday > self::DATE_SATURDAY) {
|
|
636 return;
|
|
637 }
|
|
638
|
|
639 if ($nth < 0) { // last $weekday of month
|
|
640 $this->_mday = $lastday = Horde_Date_Utils::daysInMonth($this->_month, $this->_year);
|
|
641 $last = $this->dayOfWeek();
|
|
642 $this->_mday += ($weekday - $last);
|
|
643 if ($this->_mday > $lastday)
|
|
644 $this->_mday -= 7;
|
|
645 }
|
|
646 else {
|
|
647 $this->_mday = 1;
|
|
648 $first = $this->dayOfWeek();
|
|
649 if ($weekday < $first) {
|
|
650 $this->_mday = 8 + $weekday - $first;
|
|
651 } else {
|
|
652 $this->_mday = $weekday - $first + 1;
|
|
653 }
|
|
654 $diff = 7 * $nth - 7;
|
|
655 $this->_mday += $diff;
|
|
656 $this->_correct(self::MASK_DAY, $diff < 0);
|
|
657 }
|
|
658 }
|
|
659
|
|
660 /**
|
|
661 * Is the date currently represented by this object a valid date?
|
|
662 *
|
|
663 * @return boolean Validity, counting leap years, etc.
|
|
664 */
|
|
665 public function isValid()
|
|
666 {
|
|
667 return ($this->_year >= 0 && $this->_year <= 9999);
|
|
668 }
|
|
669
|
|
670 /**
|
|
671 * Compares this date to another date object to see which one is
|
|
672 * greater (later). Assumes that the dates are in the same
|
|
673 * timezone.
|
|
674 *
|
|
675 * @param mixed $other The date to compare to.
|
|
676 *
|
|
677 * @return integer == 0 if they are on the same date
|
|
678 * >= 1 if $this is greater (later)
|
|
679 * <= -1 if $other is greater (later)
|
|
680 */
|
|
681 public function compareDate($other)
|
|
682 {
|
|
683 if (!($other instanceof Horde_Date)) {
|
|
684 $other = new Horde_Date($other);
|
|
685 }
|
|
686
|
|
687 if ($this->_year != $other->year) {
|
|
688 return $this->_year - $other->year;
|
|
689 }
|
|
690 if ($this->_month != $other->month) {
|
|
691 return $this->_month - $other->month;
|
|
692 }
|
|
693
|
|
694 return $this->_mday - $other->mday;
|
|
695 }
|
|
696
|
|
697 /**
|
|
698 * Returns whether this date is after the other.
|
|
699 *
|
|
700 * @param mixed $other The date to compare to.
|
|
701 *
|
|
702 * @return boolean True if this date is after the other.
|
|
703 */
|
|
704 public function after($other)
|
|
705 {
|
|
706 return $this->compareDate($other) > 0;
|
|
707 }
|
|
708
|
|
709 /**
|
|
710 * Returns whether this date is before the other.
|
|
711 *
|
|
712 * @param mixed $other The date to compare to.
|
|
713 *
|
|
714 * @return boolean True if this date is before the other.
|
|
715 */
|
|
716 public function before($other)
|
|
717 {
|
|
718 return $this->compareDate($other) < 0;
|
|
719 }
|
|
720
|
|
721 /**
|
|
722 * Returns whether this date is the same like the other.
|
|
723 *
|
|
724 * @param mixed $other The date to compare to.
|
|
725 *
|
|
726 * @return boolean True if this date is the same like the other.
|
|
727 */
|
|
728 public function equals($other)
|
|
729 {
|
|
730 return $this->compareDate($other) == 0;
|
|
731 }
|
|
732
|
|
733 /**
|
|
734 * Compares this to another date object by time, to see which one
|
|
735 * is greater (later). Assumes that the dates are in the same
|
|
736 * timezone.
|
|
737 *
|
|
738 * @param mixed $other The date to compare to.
|
|
739 *
|
|
740 * @return integer == 0 if they are at the same time
|
|
741 * >= 1 if $this is greater (later)
|
|
742 * <= -1 if $other is greater (later)
|
|
743 */
|
|
744 public function compareTime($other)
|
|
745 {
|
|
746 if (!($other instanceof Horde_Date)) {
|
|
747 $other = new Horde_Date($other);
|
|
748 }
|
|
749
|
|
750 if ($this->_hour != $other->hour) {
|
|
751 return $this->_hour - $other->hour;
|
|
752 }
|
|
753 if ($this->_min != $other->min) {
|
|
754 return $this->_min - $other->min;
|
|
755 }
|
|
756
|
|
757 return $this->_sec - $other->sec;
|
|
758 }
|
|
759
|
|
760 /**
|
|
761 * Compares this to another date object, including times, to see
|
|
762 * which one is greater (later). Assumes that the dates are in the
|
|
763 * same timezone.
|
|
764 *
|
|
765 * @param mixed $other The date to compare to.
|
|
766 *
|
|
767 * @return integer == 0 if they are equal
|
|
768 * >= 1 if $this is greater (later)
|
|
769 * <= -1 if $other is greater (later)
|
|
770 */
|
|
771 public function compareDateTime($other)
|
|
772 {
|
|
773 if (!($other instanceof Horde_Date)) {
|
|
774 $other = new Horde_Date($other);
|
|
775 }
|
|
776
|
|
777 if ($diff = $this->compareDate($other)) {
|
|
778 return $diff;
|
|
779 }
|
|
780
|
|
781 return $this->compareTime($other);
|
|
782 }
|
|
783
|
|
784 /**
|
|
785 * Returns number of days between this date and another.
|
|
786 *
|
|
787 * @param Horde_Date $other The other day to diff with.
|
|
788 *
|
|
789 * @return integer The absolute number of days between the two dates.
|
|
790 */
|
|
791 public function diff($other)
|
|
792 {
|
|
793 return abs($this->toDays() - $other->toDays());
|
|
794 }
|
|
795
|
|
796 /**
|
|
797 * Returns the time offset for local time zone.
|
|
798 *
|
|
799 * @param boolean $colon Place a colon between hours and minutes?
|
|
800 *
|
|
801 * @return string Timezone offset as a string in the format +HH:MM.
|
|
802 */
|
|
803 public function tzOffset($colon = true)
|
|
804 {
|
|
805 return $colon ? $this->format('P') : $this->format('O');
|
|
806 }
|
|
807
|
|
808 /**
|
|
809 * Returns the unix timestamp representation of this date.
|
|
810 *
|
|
811 * @return integer A unix timestamp.
|
|
812 */
|
|
813 public function timestamp()
|
|
814 {
|
|
815 if ($this->_year >= 1970 && $this->_year < 2038) {
|
|
816 return mktime($this->_hour, $this->_min, $this->_sec,
|
|
817 $this->_month, $this->_mday, $this->_year);
|
|
818 }
|
|
819 return $this->format('U');
|
|
820 }
|
|
821
|
|
822 /**
|
|
823 * Returns the unix timestamp representation of this date, 12:00am.
|
|
824 *
|
|
825 * @return integer A unix timestamp.
|
|
826 */
|
|
827 public function datestamp()
|
|
828 {
|
|
829 if ($this->_year >= 1970 && $this->_year < 2038) {
|
|
830 return mktime(0, 0, 0, $this->_month, $this->_mday, $this->_year);
|
|
831 }
|
|
832 $date = new DateTime($this->format('Y-m-d'));
|
|
833 return $date->format('U');
|
|
834 }
|
|
835
|
|
836 /**
|
|
837 * Formats date and time to be passed around as a short url parameter.
|
|
838 *
|
|
839 * @return string Date and time.
|
|
840 */
|
|
841 public function dateString()
|
|
842 {
|
|
843 return sprintf('%04d%02d%02d', $this->_year, $this->_month, $this->_mday);
|
|
844 }
|
|
845
|
|
846 /**
|
|
847 * Formats date and time to the ISO format used by JSON.
|
|
848 *
|
|
849 * @return string Date and time.
|
|
850 */
|
|
851 public function toJson()
|
|
852 {
|
|
853 return $this->format(self::DATE_JSON);
|
|
854 }
|
|
855
|
|
856 /**
|
|
857 * Formats date and time to the RFC 2445 iCalendar DATE-TIME format.
|
|
858 *
|
|
859 * @param boolean $floating Whether to return a floating date-time
|
|
860 * (without time zone information).
|
|
861 *
|
|
862 * @return string Date and time.
|
|
863 */
|
|
864 public function toiCalendar($floating = false)
|
|
865 {
|
|
866 if ($floating) {
|
|
867 return $this->format('Ymd\THis');
|
|
868 }
|
|
869 $dateTime = $this->toDateTime();
|
|
870 $dateTime->setTimezone(new DateTimeZone('UTC'));
|
|
871 return $dateTime->format('Ymd\THis\Z');
|
|
872 }
|
|
873
|
|
874 /**
|
|
875 * Formats time using the specifiers available in date() or in the DateTime
|
|
876 * class' format() method.
|
|
877 *
|
|
878 * To format in languages other than English, use strftime() instead.
|
|
879 *
|
|
880 * @param string $format
|
|
881 *
|
|
882 * @return string Formatted time.
|
|
883 */
|
|
884 public function format($format)
|
|
885 {
|
|
886 if (!isset($this->_formatCache[$format])) {
|
|
887 $this->_formatCache[$format] = $this->toDateTime()->format($format);
|
|
888 }
|
|
889 return $this->_formatCache[$format];
|
|
890 }
|
|
891
|
|
892 /**
|
|
893 * Formats date and time using strftime() format.
|
|
894 *
|
|
895 * @return string strftime() formatted date and time.
|
|
896 */
|
|
897 public function strftime($format)
|
|
898 {
|
|
899 if (preg_match('/%[^' . self::$_supportedSpecs . ']/', $format)) {
|
|
900 return strftime($format, $this->timestamp());
|
|
901 } else {
|
|
902 return $this->_strftime($format);
|
|
903 }
|
|
904 }
|
|
905
|
|
906 /**
|
|
907 * Formats date and time using a limited set of the strftime() format.
|
|
908 *
|
|
909 * @return string strftime() formatted date and time.
|
|
910 */
|
|
911 protected function _strftime($format)
|
|
912 {
|
|
913 return preg_replace(
|
|
914 array('/%b/e',
|
|
915 '/%B/e',
|
|
916 '/%C/e',
|
|
917 '/%d/e',
|
|
918 '/%D/e',
|
|
919 '/%e/e',
|
|
920 '/%H/e',
|
|
921 '/%I/e',
|
|
922 '/%m/e',
|
|
923 '/%M/e',
|
|
924 '/%n/',
|
|
925 '/%p/e',
|
|
926 '/%R/e',
|
|
927 '/%S/e',
|
|
928 '/%t/',
|
|
929 '/%T/e',
|
|
930 '/%x/e',
|
|
931 '/%X/e',
|
|
932 '/%y/e',
|
|
933 '/%Y/',
|
|
934 '/%%/'),
|
|
935 array('$this->_strftime(Horde_Nls::getLangInfo(constant(\'ABMON_\' . (int)$this->_month)))',
|
|
936 '$this->_strftime(Horde_Nls::getLangInfo(constant(\'MON_\' . (int)$this->_month)))',
|
|
937 '(int)($this->_year / 100)',
|
|
938 'sprintf(\'%02d\', $this->_mday)',
|
|
939 '$this->_strftime(\'%m/%d/%y\')',
|
|
940 'sprintf(\'%2d\', $this->_mday)',
|
|
941 'sprintf(\'%02d\', $this->_hour)',
|
|
942 'sprintf(\'%02d\', $this->_hour == 0 ? 12 : ($this->_hour > 12 ? $this->_hour - 12 : $this->_hour))',
|
|
943 'sprintf(\'%02d\', $this->_month)',
|
|
944 'sprintf(\'%02d\', $this->_min)',
|
|
945 "\n",
|
|
946 '$this->_strftime(Horde_Nls::getLangInfo($this->_hour < 12 ? AM_STR : PM_STR))',
|
|
947 '$this->_strftime(\'%H:%M\')',
|
|
948 'sprintf(\'%02d\', $this->_sec)',
|
|
949 "\t",
|
|
950 '$this->_strftime(\'%H:%M:%S\')',
|
|
951 '$this->_strftime(Horde_Nls::getLangInfo(D_FMT))',
|
|
952 '$this->_strftime(Horde_Nls::getLangInfo(T_FMT))',
|
|
953 'substr(sprintf(\'%04d\', $this->_year), -2)',
|
|
954 (int)$this->_year,
|
|
955 '%'),
|
|
956 $format);
|
|
957 }
|
|
958
|
|
959 /**
|
|
960 * Corrects any over- or underflows in any of the date's members.
|
|
961 *
|
|
962 * @param integer $mask We may not want to correct some overflows.
|
|
963 * @param integer $down Whether to correct the date up or down.
|
|
964 */
|
|
965 protected function _correct($mask = self::MASK_ALLPARTS, $down = false)
|
|
966 {
|
|
967 if ($mask & self::MASK_SECOND) {
|
|
968 if ($this->_sec < 0 || $this->_sec > 59) {
|
|
969 $mask |= self::MASK_MINUTE;
|
|
970
|
|
971 $this->_min += (int)($this->_sec / 60);
|
|
972 $this->_sec %= 60;
|
|
973 if ($this->_sec < 0) {
|
|
974 $this->_min--;
|
|
975 $this->_sec += 60;
|
|
976 }
|
|
977 }
|
|
978 }
|
|
979
|
|
980 if ($mask & self::MASK_MINUTE) {
|
|
981 if ($this->_min < 0 || $this->_min > 59) {
|
|
982 $mask |= self::MASK_HOUR;
|
|
983
|
|
984 $this->_hour += (int)($this->_min / 60);
|
|
985 $this->_min %= 60;
|
|
986 if ($this->_min < 0) {
|
|
987 $this->_hour--;
|
|
988 $this->_min += 60;
|
|
989 }
|
|
990 }
|
|
991 }
|
|
992
|
|
993 if ($mask & self::MASK_HOUR) {
|
|
994 if ($this->_hour < 0 || $this->_hour > 23) {
|
|
995 $mask |= self::MASK_DAY;
|
|
996
|
|
997 $this->_mday += (int)($this->_hour / 24);
|
|
998 $this->_hour %= 24;
|
|
999 if ($this->_hour < 0) {
|
|
1000 $this->_mday--;
|
|
1001 $this->_hour += 24;
|
|
1002 }
|
|
1003 }
|
|
1004 }
|
|
1005
|
|
1006 if ($mask & self::MASK_MONTH) {
|
|
1007 $this->_correctMonth($down);
|
|
1008 /* When correcting the month, always correct the day too. Months
|
|
1009 * have different numbers of days. */
|
|
1010 $mask |= self::MASK_DAY;
|
|
1011 }
|
|
1012
|
|
1013 if ($mask & self::MASK_DAY) {
|
|
1014 while ($this->_mday > 28 &&
|
|
1015 $this->_mday > Horde_Date_Utils::daysInMonth($this->_month, $this->_year)) {
|
|
1016 if ($down) {
|
|
1017 $this->_mday -= Horde_Date_Utils::daysInMonth($this->_month + 1, $this->_year) - Horde_Date_Utils::daysInMonth($this->_month, $this->_year);
|
|
1018 } else {
|
|
1019 $this->_mday -= Horde_Date_Utils::daysInMonth($this->_month, $this->_year);
|
|
1020 $this->_month++;
|
|
1021 }
|
|
1022 $this->_correctMonth($down);
|
|
1023 }
|
|
1024 while ($this->_mday < 1) {
|
|
1025 --$this->_month;
|
|
1026 $this->_correctMonth($down);
|
|
1027 $this->_mday += Horde_Date_Utils::daysInMonth($this->_month, $this->_year);
|
|
1028 }
|
|
1029 }
|
|
1030 }
|
|
1031
|
|
1032 /**
|
|
1033 * Corrects the current month.
|
|
1034 *
|
|
1035 * This cannot be done in _correct() because that would also trigger a
|
|
1036 * correction of the day, which would result in an infinite loop.
|
|
1037 *
|
|
1038 * @param integer $down Whether to correct the date up or down.
|
|
1039 */
|
|
1040 protected function _correctMonth($down = false)
|
|
1041 {
|
|
1042 $this->_year += (int)($this->_month / 12);
|
|
1043 $this->_month %= 12;
|
|
1044 if ($this->_month < 1) {
|
|
1045 $this->_year--;
|
|
1046 $this->_month += 12;
|
|
1047 }
|
|
1048 }
|
|
1049
|
|
1050 /**
|
|
1051 * Handles args in order: year month day hour min sec tz
|
|
1052 */
|
|
1053 protected function _initializeFromArgs($args)
|
|
1054 {
|
|
1055 $tz = (isset($args[6])) ? array_pop($args) : null;
|
|
1056 $this->_initializeTimezone($tz);
|
|
1057
|
|
1058 $args = array_slice($args, 0, 6);
|
|
1059 $keys = array('year' => 1, 'month' => 1, 'mday' => 1, 'hour' => 0, 'min' => 0, 'sec' => 0);
|
|
1060 $date = array_combine(array_slice(array_keys($keys), 0, count($args)), $args);
|
|
1061 $date = array_merge($keys, $date);
|
|
1062
|
|
1063 $this->_initializeFromArray($date);
|
|
1064 }
|
|
1065
|
|
1066 protected function _initializeFromArray($date)
|
|
1067 {
|
|
1068 if (isset($date['year']) && is_string($date['year']) && strlen($date['year']) == 2) {
|
|
1069 if ($date['year'] > 70) {
|
|
1070 $date['year'] += 1900;
|
|
1071 } else {
|
|
1072 $date['year'] += 2000;
|
|
1073 }
|
|
1074 }
|
|
1075
|
|
1076 foreach ($date as $key => $val) {
|
|
1077 if (in_array($key, array('year', 'month', 'mday', 'hour', 'min', 'sec'))) {
|
|
1078 $this->{'_'. $key} = (int)$val;
|
|
1079 }
|
|
1080 }
|
|
1081
|
|
1082 // If $date['day'] is present and numeric we may have been passed
|
|
1083 // a Horde_Form_datetime array.
|
|
1084 if (isset($date['day']) &&
|
|
1085 (string)(int)$date['day'] == $date['day']) {
|
|
1086 $this->_mday = (int)$date['day'];
|
|
1087 }
|
|
1088 // 'minute' key also from Horde_Form_datetime
|
|
1089 if (isset($date['minute']) &&
|
|
1090 (string)(int)$date['minute'] == $date['minute']) {
|
|
1091 $this->_min = (int)$date['minute'];
|
|
1092 }
|
|
1093
|
|
1094 $this->_correct();
|
|
1095 }
|
|
1096
|
|
1097 protected function _initializeFromObject($date)
|
|
1098 {
|
|
1099 if ($date instanceof DateTime) {
|
|
1100 $this->_year = (int)$date->format('Y');
|
|
1101 $this->_month = (int)$date->format('m');
|
|
1102 $this->_mday = (int)$date->format('d');
|
|
1103 $this->_hour = (int)$date->format('H');
|
|
1104 $this->_min = (int)$date->format('i');
|
|
1105 $this->_sec = (int)$date->format('s');
|
|
1106 $this->_initializeTimezone($date->getTimezone()->getName());
|
|
1107 } else {
|
|
1108 $is_horde_date = $date instanceof Horde_Date;
|
|
1109 foreach (array('year', 'month', 'mday', 'hour', 'min', 'sec') as $key) {
|
|
1110 if ($is_horde_date || isset($date->$key)) {
|
|
1111 $this->{'_' . $key} = (int)$date->$key;
|
|
1112 }
|
|
1113 }
|
|
1114 if (!$is_horde_date) {
|
|
1115 $this->_correct();
|
|
1116 } else {
|
|
1117 $this->_initializeTimezone($date->timezone);
|
|
1118 }
|
|
1119 }
|
|
1120 }
|
|
1121
|
|
1122 protected function _initializeTimezone($timezone)
|
|
1123 {
|
|
1124 if (empty($timezone)) {
|
|
1125 $timezone = date_default_timezone_get();
|
|
1126 }
|
|
1127 $this->_timezone = $timezone;
|
|
1128 }
|
|
1129
|
|
1130 }
|
|
1131
|
|
1132 /**
|
|
1133 * @category Horde
|
|
1134 * @package Date
|
|
1135 */
|
|
1136
|
|
1137 /**
|
|
1138 * Horde Date wrapper/logic class, including some calculation
|
|
1139 * functions.
|
|
1140 *
|
|
1141 * @category Horde
|
|
1142 * @package Date
|
|
1143 */
|
|
1144 class Horde_Date_Utils
|
|
1145 {
|
|
1146 /**
|
|
1147 * Returns whether a year is a leap year.
|
|
1148 *
|
|
1149 * @param integer $year The year.
|
|
1150 *
|
|
1151 * @return boolean True if the year is a leap year.
|
|
1152 */
|
|
1153 public static function isLeapYear($year)
|
|
1154 {
|
|
1155 if (strlen($year) != 4 || preg_match('/\D/', $year)) {
|
|
1156 return false;
|
|
1157 }
|
|
1158
|
|
1159 return (($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0);
|
|
1160 }
|
|
1161
|
|
1162 /**
|
|
1163 * Returns the date of the year that corresponds to the first day of the
|
|
1164 * given week.
|
|
1165 *
|
|
1166 * @param integer $week The week of the year to find the first day of.
|
|
1167 * @param integer $year The year to calculate for.
|
|
1168 *
|
|
1169 * @return Horde_Date The date of the first day of the given week.
|
|
1170 */
|
|
1171 public static function firstDayOfWeek($week, $year)
|
|
1172 {
|
|
1173 return new Horde_Date(sprintf('%04dW%02d', $year, $week));
|
|
1174 }
|
|
1175
|
|
1176 /**
|
|
1177 * Returns the number of days in the specified month.
|
|
1178 *
|
|
1179 * @param integer $month The month
|
|
1180 * @param integer $year The year.
|
|
1181 *
|
|
1182 * @return integer The number of days in the month.
|
|
1183 */
|
|
1184 public static function daysInMonth($month, $year)
|
|
1185 {
|
|
1186 static $cache = array();
|
|
1187 if (!isset($cache[$year][$month])) {
|
|
1188 $date = new DateTime(sprintf('%04d-%02d-01', $year, $month));
|
|
1189 $cache[$year][$month] = $date->format('t');
|
|
1190 }
|
|
1191 return $cache[$year][$month];
|
|
1192 }
|
|
1193
|
|
1194 /**
|
|
1195 * Returns a relative, natural language representation of a timestamp
|
|
1196 *
|
|
1197 * @todo Wider range of values ... maybe future time as well?
|
|
1198 * @todo Support minimum resolution parameter.
|
|
1199 *
|
|
1200 * @param mixed $time The time. Any format accepted by Horde_Date.
|
|
1201 * @param string $date_format Format to display date if timestamp is
|
|
1202 * more then 1 day old.
|
|
1203 * @param string $time_format Format to display time if timestamp is 1
|
|
1204 * day old.
|
|
1205 *
|
|
1206 * @return string The relative time (i.e. 2 minutes ago)
|
|
1207 */
|
|
1208 public static function relativeDateTime($time, $date_format = '%x',
|
|
1209 $time_format = '%X')
|
|
1210 {
|
|
1211 $date = new Horde_Date($time);
|
|
1212
|
|
1213 $delta = time() - $date->timestamp();
|
|
1214 if ($delta < 60) {
|
|
1215 return sprintf(Horde_Date_Translation::ngettext("%d second ago", "%d seconds ago", $delta), $delta);
|
|
1216 }
|
|
1217
|
|
1218 $delta = round($delta / 60);
|
|
1219 if ($delta < 60) {
|
|
1220 return sprintf(Horde_Date_Translation::ngettext("%d minute ago", "%d minutes ago", $delta), $delta);
|
|
1221 }
|
|
1222
|
|
1223 $delta = round($delta / 60);
|
|
1224 if ($delta < 24) {
|
|
1225 return sprintf(Horde_Date_Translation::ngettext("%d hour ago", "%d hours ago", $delta), $delta);
|
|
1226 }
|
|
1227
|
|
1228 if ($delta > 24 && $delta < 48) {
|
|
1229 $date = new Horde_Date($time);
|
|
1230 return sprintf(Horde_Date_Translation::t("yesterday at %s"), $date->strftime($time_format));
|
|
1231 }
|
|
1232
|
|
1233 $delta = round($delta / 24);
|
|
1234 if ($delta < 7) {
|
|
1235 return sprintf(Horde_Date_Translation::t("%d days ago"), $delta);
|
|
1236 }
|
|
1237
|
|
1238 if (round($delta / 7) < 5) {
|
|
1239 $delta = round($delta / 7);
|
|
1240 return sprintf(Horde_Date_Translation::ngettext("%d week ago", "%d weeks ago", $delta), $delta);
|
|
1241 }
|
|
1242
|
|
1243 // Default to the user specified date format.
|
|
1244 return $date->strftime($date_format);
|
|
1245 }
|
|
1246
|
|
1247 /**
|
|
1248 * Tries to convert strftime() formatters to date() formatters.
|
|
1249 *
|
|
1250 * Unsupported formatters will be removed.
|
|
1251 *
|
|
1252 * @param string $format A strftime() formatting string.
|
|
1253 *
|
|
1254 * @return string A date() formatting string.
|
|
1255 */
|
|
1256 public static function strftime2date($format)
|
|
1257 {
|
|
1258 $replace = array(
|
|
1259 '/%a/' => 'D',
|
|
1260 '/%A/' => 'l',
|
|
1261 '/%d/' => 'd',
|
|
1262 '/%e/' => 'j',
|
|
1263 '/%j/' => 'z',
|
|
1264 '/%u/' => 'N',
|
|
1265 '/%w/' => 'w',
|
|
1266 '/%U/' => '',
|
|
1267 '/%V/' => 'W',
|
|
1268 '/%W/' => '',
|
|
1269 '/%b/' => 'M',
|
|
1270 '/%B/' => 'F',
|
|
1271 '/%h/' => 'M',
|
|
1272 '/%m/' => 'm',
|
|
1273 '/%C/' => '',
|
|
1274 '/%g/' => '',
|
|
1275 '/%G/' => 'o',
|
|
1276 '/%y/' => 'y',
|
|
1277 '/%Y/' => 'Y',
|
|
1278 '/%H/' => 'H',
|
|
1279 '/%I/' => 'h',
|
|
1280 '/%i/' => 'g',
|
|
1281 '/%M/' => 'i',
|
|
1282 '/%p/' => 'A',
|
|
1283 '/%P/' => 'a',
|
|
1284 '/%r/' => 'h:i:s A',
|
|
1285 '/%R/' => 'H:i',
|
|
1286 '/%S/' => 's',
|
|
1287 '/%T/' => 'H:i:s',
|
|
1288 '/%X/e' => 'Horde_Date_Utils::strftime2date(Horde_Nls::getLangInfo(T_FMT))',
|
|
1289 '/%z/' => 'O',
|
|
1290 '/%Z/' => '',
|
|
1291 '/%c/' => '',
|
|
1292 '/%D/' => 'm/d/y',
|
|
1293 '/%F/' => 'Y-m-d',
|
|
1294 '/%s/' => 'U',
|
|
1295 '/%x/e' => 'Horde_Date_Utils::strftime2date(Horde_Nls::getLangInfo(D_FMT))',
|
|
1296 '/%n/' => "\n",
|
|
1297 '/%t/' => "\t",
|
|
1298 '/%%/' => '%'
|
|
1299 );
|
|
1300
|
|
1301 return preg_replace(array_keys($replace), array_values($replace), $format);
|
|
1302 }
|
|
1303
|
|
1304 }
|