Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DateObject.php
Go to the documentation of this file.
1 <?php
29 abstract class Zend_Date_DateObject {
30 
34  private $_unixTimestamp;
35  protected static $_cache = null;
36  protected static $_cacheTags = false;
37  protected static $_defaultOffset = 0;
38 
42  private $_timezone = 'UTC';
43  private $_offset = 0;
44  private $_syncronised = 0;
45 
46  // turn off DST correction if UTC or GMT
47  protected $_dst = true;
48 
52  private static $_monthTable = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
53 
57  private static $_yearTable = array(
58  1970 => 0, 1960 => -315619200, 1950 => -631152000,
59  1940 => -946771200, 1930 => -1262304000, 1920 => -1577923200,
60  1910 => -1893456000, 1900 => -2208988800, 1890 => -2524521600,
61  1880 => -2840140800, 1870 => -3155673600, 1860 => -3471292800,
62  1850 => -3786825600, 1840 => -4102444800, 1830 => -4417977600,
63  1820 => -4733596800, 1810 => -5049129600, 1800 => -5364662400,
64  1790 => -5680195200, 1780 => -5995814400, 1770 => -6311347200,
65  1760 => -6626966400, 1750 => -6942499200, 1740 => -7258118400,
66  1730 => -7573651200, 1720 => -7889270400, 1710 => -8204803200,
67  1700 => -8520336000, 1690 => -8835868800, 1680 => -9151488000,
68  1670 => -9467020800, 1660 => -9782640000, 1650 => -10098172800,
69  1640 => -10413792000, 1630 => -10729324800, 1620 => -11044944000,
70  1610 => -11360476800, 1600 => -11676096000);
71 
79  protected function setUnixTimestamp($timestamp = null)
80  {
81  $old = $this->_unixTimestamp;
82 
83  if (is_numeric($timestamp)) {
84  $this->_unixTimestamp = $timestamp;
85  } else if ($timestamp === null) {
86  $this->_unixTimestamp = time();
87  } else {
88  #require_once 'Zend/Date/Exception.php';
89  throw new Zend_Date_Exception('\'' . $timestamp . '\' is not a valid UNIX timestamp', 0, null, $timestamp);
90  }
91 
92  return $old;
93  }
94 
102  protected function getUnixTimestamp()
103  {
104  if ($this->_unixTimestamp === intval($this->_unixTimestamp)) {
105  return (int) $this->_unixTimestamp;
106  } else {
107  return (string) $this->_unixTimestamp;
108  }
109  }
110 
119  protected function _getTime($sync = null)
120  {
121  if ($sync !== null) {
122  $this->_syncronised = round($sync);
123  }
124  return (time() + $this->_syncronised);
125  }
126 
146  protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
147  {
148  // complete date but in 32bit timestamp - use PHP internal
149  if ((1901 < $year) and ($year < 2038)) {
150 
151  $oldzone = @date_default_timezone_get();
152  // Timezone also includes DST settings, therefor substracting the GMT offset is not enough
153  // We have to set the correct timezone to get the right value
154  if (($this->_timezone != $oldzone) and ($gmt === false)) {
155  date_default_timezone_set($this->_timezone);
156  }
157  $result = ($gmt) ? @gmmktime($hour, $minute, $second, $month, $day, $year)
158  : @mktime($hour, $minute, $second, $month, $day, $year);
159  date_default_timezone_set($oldzone);
160 
161  return $result;
162  }
163 
164  if ($gmt !== true) {
165  $second += $this->_offset;
166  }
167 
168  if (isset(self::$_cache)) {
169  $id = strtr('Zend_DateObject_mkTime_' . $this->_offset . '_' . $year.$month.$day.'_'.$hour.$minute.$second . '_'.(int)$gmt, '-','_');
170  if ($result = self::$_cache->load($id)) {
171  return unserialize($result);
172  }
173  }
174 
175  // date to integer
176  $day = intval($day);
177  $month = intval($month);
178  $year = intval($year);
179 
180  // correct months > 12 and months < 1
181  if ($month > 12) {
182  $overlap = floor($month / 12);
183  $year += $overlap;
184  $month -= $overlap * 12;
185  } else {
186  $overlap = ceil((1 - $month) / 12);
187  $year -= $overlap;
188  $month += $overlap * 12;
189  }
190 
191  $date = 0;
192  if ($year >= 1970) {
193 
194  // Date is after UNIX epoch
195  // go through leapyears
196  // add months from latest given year
197  for ($count = 1970; $count <= $year; $count++) {
198 
199  $leapyear = self::isYearLeapYear($count);
200  if ($count < $year) {
201 
202  $date += 365;
203  if ($leapyear === true) {
204  $date++;
205  }
206 
207  } else {
208 
209  for ($mcount = 0; $mcount < ($month - 1); $mcount++) {
210  $date += self::$_monthTable[$mcount];
211  if (($leapyear === true) and ($mcount == 1)) {
212  $date++;
213  }
214 
215  }
216  }
217  }
218 
219  $date += $day - 1;
220  $date = (($date * 86400) + ($hour * 3600) + ($minute * 60) + $second);
221  } else {
222 
223  // Date is before UNIX epoch
224  // go through leapyears
225  // add months from latest given year
226  for ($count = 1969; $count >= $year; $count--) {
227 
228  $leapyear = self::isYearLeapYear($count);
229  if ($count > $year)
230  {
231  $date += 365;
232  if ($leapyear === true)
233  $date++;
234  } else {
235 
236  for ($mcount = 11; $mcount > ($month - 1); $mcount--) {
237  $date += self::$_monthTable[$mcount];
238  if (($leapyear === true) and ($mcount == 2)) {
239  $date++;
240  }
241 
242  }
243  }
244  }
245 
246  $date += (self::$_monthTable[$month - 1] - $day);
247  $date = -(($date * 86400) + (86400 - (($hour * 3600) + ($minute * 60) + $second)));
248 
249  // gregorian correction for 5.Oct.1582
250  if ($date < -12220185600) {
251  $date += 864000;
252  } else if ($date < -12219321600) {
253  $date = -12219321600;
254  }
255  }
256 
257  if (isset(self::$_cache)) {
258  if (self::$_cacheTags) {
259  self::$_cache->save( serialize($date), $id, array('Zend_Date'));
260  } else {
261  self::$_cache->save( serialize($date), $id);
262  }
263  }
264 
265  return $date;
266  }
267 
274  protected static function isYearLeapYear($year)
275  {
276  // all leapyears can be divided through 4
277  if (($year % 4) != 0) {
278  return false;
279  }
280 
281  // all leapyears can be divided through 400
282  if ($year % 400 == 0) {
283  return true;
284  } else if (($year > 1582) and ($year % 100 == 0)) {
285  return false;
286  }
287 
288  return true;
289  }
290 
301  protected function date($format, $timestamp = null, $gmt = false)
302  {
303  $oldzone = @date_default_timezone_get();
304  if ($this->_timezone != $oldzone) {
305  date_default_timezone_set($this->_timezone);
306  }
307 
308  if ($timestamp === null) {
309  $result = ($gmt) ? @gmdate($format) : @date($format);
310  date_default_timezone_set($oldzone);
311  return $result;
312  }
313 
314  if (abs($timestamp) <= 0x7FFFFFFF) {
315  // See ZF-11992
316  // "o" will sometimes resolve to the previous year (see
317  // http://php.net/date ; it's part of the ISO 8601
318  // standard). However, this is not desired, so replacing
319  // all occurrences of "o" not preceded by a backslash
320  // with "Y"
321  $format = preg_replace('/(?<!\\\\)o/', 'Y', $format);
322  $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
323  date_default_timezone_set($oldzone);
324  return $result;
325  }
326 
327  $jump = false;
328  $origstamp = $timestamp;
329  if (isset(self::$_cache)) {
330  $idstamp = strtr('Zend_DateObject_date_' . $this->_offset . '_'. $timestamp . '_'.(int)$gmt, '-','_');
331  if ($result2 = self::$_cache->load($idstamp)) {
332  $timestamp = unserialize($result2);
333  $jump = true;
334  }
335  }
336 
337  // check on false or null alone fails
338  if (empty($gmt) and empty($jump)) {
339  $tempstamp = $timestamp;
340  if ($tempstamp > 0) {
341  while (abs($tempstamp) > 0x7FFFFFFF) {
342  $tempstamp -= (86400 * 23376);
343  }
344 
345  $dst = date("I", $tempstamp);
346  if ($dst === 1) {
347  $timestamp += 3600;
348  }
349 
350  $temp = date('Z', $tempstamp);
351  $timestamp += $temp;
352  }
353 
354  if (isset(self::$_cache)) {
355  if (self::$_cacheTags) {
356  self::$_cache->save( serialize($timestamp), $idstamp, array('Zend_Date'));
357  } else {
358  self::$_cache->save( serialize($timestamp), $idstamp);
359  }
360  }
361  }
362 
363  if (($timestamp < 0) and ($gmt !== true)) {
364  $timestamp -= $this->_offset;
365  }
366 
367  date_default_timezone_set($oldzone);
368  $date = $this->getDateParts($timestamp, true);
369  $length = strlen($format);
370  $output = '';
371 
372  for ($i = 0; $i < $length; $i++) {
373  switch($format[$i]) {
374  // day formats
375  case 'd': // day of month, 2 digits, with leading zero, 01 - 31
376  $output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
377  break;
378 
379  case 'D': // day of week, 3 letters, Mon - Sun
380  $output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
381  break;
382 
383  case 'j': // day of month, without leading zero, 1 - 31
384  $output .= $date['mday'];
385  break;
386 
387  case 'l': // day of week, full string name, Sunday - Saturday
388  $output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
389  break;
390 
391  case 'N': // ISO 8601 numeric day of week, 1 - 7
392  $day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
393  if ($day == 0) {
394  $day = 7;
395  }
396  $output .= $day;
397  break;
398 
399  case 'S': // english suffix for day of month, st nd rd th
400  if (($date['mday'] % 10) == 1) {
401  $output .= 'st';
402  } else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
403  $output .= 'nd';
404  } else if (($date['mday'] % 10) == 3) {
405  $output .= 'rd';
406  } else {
407  $output .= 'th';
408  }
409  break;
410 
411  case 'w': // numeric day of week, 0 - 6
412  $output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
413  break;
414 
415  case 'z': // day of year, 0 - 365
416  $output .= $date['yday'];
417  break;
418 
419 
420  // week formats
421  case 'W': // ISO 8601, week number of year
422  $output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
423  break;
424 
425 
426  // month formats
427  case 'F': // string month name, january - december
428  $output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
429  break;
430 
431  case 'm': // number of month, with leading zeros, 01 - 12
432  $output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
433  break;
434 
435  case 'M': // 3 letter month name, Jan - Dec
436  $output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
437  break;
438 
439  case 'n': // number of month, without leading zeros, 1 - 12
440  $output .= $date['mon'];
441  break;
442 
443  case 't': // number of day in month
444  $output .= self::$_monthTable[$date['mon'] - 1];
445  break;
446 
447 
448  // year formats
449  case 'L': // is leap year ?
450  $output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
451  break;
452 
453  case 'o': // ISO 8601 year number
454  $week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
455  if (($week > 50) and ($date['mon'] == 1)) {
456  $output .= ($date['year'] - 1);
457  } else {
458  $output .= $date['year'];
459  }
460  break;
461 
462  case 'Y': // year number, 4 digits
463  $output .= $date['year'];
464  break;
465 
466  case 'y': // year number, 2 digits
467  $output .= substr($date['year'], strlen($date['year']) - 2, 2);
468  break;
469 
470 
471  // time formats
472  case 'a': // lower case am/pm
473  $output .= (($date['hours'] >= 12) ? 'pm' : 'am');
474  break;
475 
476  case 'A': // upper case am/pm
477  $output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
478  break;
479 
480  case 'B': // swatch internet time
481  $dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
482  if ($gmt === true) {
483  $dayseconds += 3600;
484  }
485  $output .= (int) (($dayseconds % 86400) / 86.4);
486  break;
487 
488  case 'g': // hours without leading zeros, 12h format
489  if ($date['hours'] > 12) {
490  $hour = $date['hours'] - 12;
491  } else {
492  if ($date['hours'] == 0) {
493  $hour = '12';
494  } else {
495  $hour = $date['hours'];
496  }
497  }
498  $output .= $hour;
499  break;
500 
501  case 'G': // hours without leading zeros, 24h format
502  $output .= $date['hours'];
503  break;
504 
505  case 'h': // hours with leading zeros, 12h format
506  if ($date['hours'] > 12) {
507  $hour = $date['hours'] - 12;
508  } else {
509  if ($date['hours'] == 0) {
510  $hour = '12';
511  } else {
512  $hour = $date['hours'];
513  }
514  }
515  $output .= (($hour < 10) ? '0'.$hour : $hour);
516  break;
517 
518  case 'H': // hours with leading zeros, 24h format
519  $output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
520  break;
521 
522  case 'i': // minutes with leading zeros
523  $output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
524  break;
525 
526  case 's': // seconds with leading zeros
527  $output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
528  break;
529 
530 
531  // timezone formats
532  case 'e': // timezone identifier
533  if ($gmt === true) {
534  $output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
535  $date['mon'], $date['mday'], 2000));
536  } else {
537  $output .= date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
538  $date['mon'], $date['mday'], 2000));
539  }
540  break;
541 
542  case 'I': // daylight saving time or not
543  if ($gmt === true) {
544  $output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
545  $date['mon'], $date['mday'], 2000));
546  } else {
547  $output .= date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
548  $date['mon'], $date['mday'], 2000));
549  }
550  break;
551 
552  case 'O': // difference to GMT in hours
553  $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
554  $output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
555  break;
556 
557  case 'P': // difference to GMT with colon
558  $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
559  $gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
560  $output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
561  break;
562 
563  case 'T': // timezone settings
564  if ($gmt === true) {
565  $output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
566  $date['mon'], $date['mday'], 2000));
567  } else {
568  $output .= date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
569  $date['mon'], $date['mday'], 2000));
570  }
571  break;
572 
573  case 'Z': // timezone offset in seconds
574  $output .= ($gmt === true) ? 0 : -$this->getGmtOffset();
575  break;
576 
577 
578  // complete time formats
579  case 'c': // ISO 8601 date format
580  $difference = $this->getGmtOffset();
581  $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
582  $difference = substr($difference, 0, 3) . ':' . substr($difference, 3);
583  $output .= $date['year'] . '-'
584  . (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']) . '-'
585  . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . 'T'
586  . (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']) . ':'
587  . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
588  . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
589  . $difference;
590  break;
591 
592  case 'r': // RFC 2822 date format
593  $difference = $this->getGmtOffset();
594  $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
595  $output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) . ', '
596  . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . ' '
597  . date('M', mktime(0, 0, 0, $date['mon'], 2, 1971)) . ' '
598  . $date['year'] . ' '
599  . (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']) . ':'
600  . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
601  . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']) . ' '
602  . $difference;
603  break;
604 
605  case 'U': // Unix timestamp
606  $output .= $origstamp;
607  break;
608 
609 
610  // special formats
611  case "\\": // next letter to print with no format
612  $i++;
613  if ($i < $length) {
614  $output .= $format[$i];
615  }
616  break;
617 
618  default: // letter is no format so add it direct
619  $output .= $format[$i];
620  break;
621  }
622  }
623 
624  return (string) $output;
625  }
626 
636  protected static function dayOfWeek($year, $month, $day)
637  {
638  if ((1901 < $year) and ($year < 2038)) {
639  return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
640  }
641 
642  // gregorian correction
643  $correction = 0;
644  if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
645  $correction = 3;
646  }
647 
648  if ($month > 2) {
649  $month -= 2;
650  } else {
651  $month += 10;
652  $year--;
653  }
654 
655  $day = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
656  $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
657 
658  return (int) ($day - 7 * floor($day / 7));
659  }
660 
674  protected function getDateParts($timestamp = null, $fast = null)
675  {
676 
677  // actual timestamp
678  if (!is_numeric($timestamp)) {
679  return getdate();
680  }
681 
682  // 32bit timestamp
683  if (abs($timestamp) <= 0x7FFFFFFF) {
684  return @getdate((int) $timestamp);
685  }
686 
687  if (isset(self::$_cache)) {
688  $id = strtr('Zend_DateObject_getDateParts_' . $timestamp.'_'.(int)$fast, '-','_');
689  if ($result = self::$_cache->load($id)) {
690  return unserialize($result);
691  }
692  }
693 
694  $otimestamp = $timestamp;
695  $numday = 0;
696  $month = 0;
697  // gregorian correction
698  if ($timestamp < -12219321600) {
699  $timestamp -= 864000;
700  }
701 
702  // timestamp lower 0
703  if ($timestamp < 0) {
704  $sec = 0;
705  $act = 1970;
706 
707  // iterate through 10 years table, increasing speed
708  foreach(self::$_yearTable as $year => $seconds) {
709  if ($timestamp >= $seconds) {
710  $i = $act;
711  break;
712  }
713  $sec = $seconds;
714  $act = $year;
715  }
716 
717  $timestamp -= $sec;
718  if (!isset($i)) {
719  $i = $act;
720  }
721 
722  // iterate the max last 10 years
723  do {
724  --$i;
725  $day = $timestamp;
726 
727  $timestamp += 31536000;
728  $leapyear = self::isYearLeapYear($i);
729  if ($leapyear === true) {
730  $timestamp += 86400;
731  }
732 
733  if ($timestamp >= 0) {
734  $year = $i;
735  break;
736  }
737  } while ($timestamp < 0);
738 
739  $secondsPerYear = 86400 * ($leapyear ? 366 : 365) + $day;
740 
741  $timestamp = $day;
742  // iterate through months
743  for ($i = 12; --$i >= 0;) {
744  $day = $timestamp;
745 
746  $timestamp += self::$_monthTable[$i] * 86400;
747  if (($leapyear === true) and ($i == 1)) {
748  $timestamp += 86400;
749  }
750 
751  if ($timestamp >= 0) {
752  $month = $i;
753  $numday = self::$_monthTable[$i];
754  if (($leapyear === true) and ($i == 1)) {
755  ++$numday;
756  }
757  break;
758  }
759  }
760 
761  $timestamp = $day;
762  $numberdays = $numday + ceil(($timestamp + 1) / 86400);
763 
764  $timestamp += ($numday - $numberdays + 1) * 86400;
765  $hours = floor($timestamp / 3600);
766  } else {
767 
768  // iterate through years
769  for ($i = 1970;;$i++) {
770  $day = $timestamp;
771 
772  $timestamp -= 31536000;
773  $leapyear = self::isYearLeapYear($i);
774  if ($leapyear === true) {
775  $timestamp -= 86400;
776  }
777 
778  if ($timestamp < 0) {
779  $year = $i;
780  break;
781  }
782  }
783 
784  $secondsPerYear = $day;
785 
786  $timestamp = $day;
787  // iterate through months
788  for ($i = 0; $i <= 11; $i++) {
789  $day = $timestamp;
790  $timestamp -= self::$_monthTable[$i] * 86400;
791 
792  if (($leapyear === true) and ($i == 1)) {
793  $timestamp -= 86400;
794  }
795 
796  if ($timestamp < 0) {
797  $month = $i;
798  $numday = self::$_monthTable[$i];
799  if (($leapyear === true) and ($i == 1)) {
800  ++$numday;
801  }
802  break;
803  }
804  }
805 
806  $timestamp = $day;
807  $numberdays = ceil(($timestamp + 1) / 86400);
808  $timestamp = $timestamp - ($numberdays - 1) * 86400;
809  $hours = floor($timestamp / 3600);
810  }
811 
812  $timestamp -= $hours * 3600;
813 
814  $month += 1;
815  $minutes = floor($timestamp / 60);
816  $seconds = $timestamp - $minutes * 60;
817 
818  if ($fast === true) {
819  $array = array(
820  'seconds' => $seconds,
821  'minutes' => $minutes,
822  'hours' => $hours,
823  'mday' => $numberdays,
824  'mon' => $month,
825  'year' => $year,
826  'yday' => floor($secondsPerYear / 86400),
827  );
828  } else {
829 
830  $dayofweek = self::dayOfWeek($year, $month, $numberdays);
831  $array = array(
832  'seconds' => $seconds,
833  'minutes' => $minutes,
834  'hours' => $hours,
835  'mday' => $numberdays,
836  'wday' => $dayofweek,
837  'mon' => $month,
838  'year' => $year,
839  'yday' => floor($secondsPerYear / 86400),
840  'weekday' => gmdate('l', 86400 * (3 + $dayofweek)),
841  'month' => gmdate('F', mktime(0, 0, 0, $month, 1, 1971)),
842  0 => $otimestamp
843  );
844  }
845 
846  if (isset(self::$_cache)) {
847  if (self::$_cacheTags) {
848  self::$_cache->save( serialize($array), $id, array('Zend_Date'));
849  } else {
850  self::$_cache->save( serialize($array), $id);
851  }
852  }
853 
854  return $array;
855  }
856 
867  protected function weekNumber($year, $month, $day)
868  {
869  if ((1901 < $year) and ($year < 2038)) {
870  return (int) date('W', mktime(0, 0, 0, $month, $day, $year));
871  }
872 
873  $dayofweek = self::dayOfWeek($year, $month, $day);
874  $firstday = self::dayOfWeek($year, 1, 1);
875  if (($month == 1) and (($firstday < 1) or ($firstday > 4)) and ($day < 4)) {
876  $firstday = self::dayOfWeek($year - 1, 1, 1);
877  $month = 12;
878  $day = 31;
879 
880  } else if (($month == 12) and ((self::dayOfWeek($year + 1, 1, 1) < 5) and
881  (self::dayOfWeek($year + 1, 1, 1) > 0))) {
882  return 1;
883  }
884 
885  return intval (((self::dayOfWeek($year, 1, 1) < 5) and (self::dayOfWeek($year, 1, 1) > 0)) +
886  4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
887  }
888 
896  private function _range($a, $b) {
897  while ($a < 0) {
898  $a += $b;
899  }
900  while ($a >= $b) {
901  $a -= $b;
902  }
903  return $a;
904  }
905 
913  protected function calcSun($location, $horizon, $rise = false)
914  {
915  // timestamp within 32bit
916  if (abs($this->_unixTimestamp) <= 0x7FFFFFFF) {
917  if ($rise === false) {
918  return date_sunset($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
919  $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
920  }
921  return date_sunrise($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
922  $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
923  }
924 
925  // self calculation - timestamp bigger than 32bit
926  // fix circle values
927  $quarterCircle = 0.5 * M_PI;
928  $halfCircle = M_PI;
929  $threeQuarterCircle = 1.5 * M_PI;
930  $fullCircle = 2 * M_PI;
931 
932  // radiant conversion for coordinates
933  $radLatitude = $location['latitude'] * $halfCircle / 180;
934  $radLongitude = $location['longitude'] * $halfCircle / 180;
935 
936  // get solar coordinates
937  $tmpRise = $rise ? $quarterCircle : $threeQuarterCircle;
938  $radDay = $this->date('z',$this->_unixTimestamp) + ($tmpRise - $radLongitude) / $fullCircle;
939 
940  // solar anomoly and longitude
941  $solAnomoly = $radDay * 0.017202 - 0.0574039;
942  $solLongitude = $solAnomoly + 0.0334405 * sin($solAnomoly);
943  $solLongitude += 4.93289 + 3.49066E-4 * sin(2 * $solAnomoly);
944 
945  // get quadrant
946  $solLongitude = $this->_range($solLongitude, $fullCircle);
947 
948  if (($solLongitude / $quarterCircle) - intval($solLongitude / $quarterCircle) == 0) {
949  $solLongitude += 4.84814E-6;
950  }
951 
952  // solar ascension
953  $solAscension = sin($solLongitude) / cos($solLongitude);
954  $solAscension = atan2(0.91746 * $solAscension, 1);
955 
956  // adjust quadrant
957  if ($solLongitude > $threeQuarterCircle) {
958  $solAscension += $fullCircle;
959  } else if ($solLongitude > $quarterCircle) {
960  $solAscension += $halfCircle;
961  }
962 
963  // solar declination
964  $solDeclination = 0.39782 * sin($solLongitude);
965  $solDeclination /= sqrt(-$solDeclination * $solDeclination + 1);
966  $solDeclination = atan2($solDeclination, 1);
967 
968  $solHorizon = $horizon - sin($solDeclination) * sin($radLatitude);
969  $solHorizon /= cos($solDeclination) * cos($radLatitude);
970 
971  // midnight sun, always night
972  if (abs($solHorizon) > 1) {
973  return false;
974  }
975 
976  $solHorizon /= sqrt(-$solHorizon * $solHorizon + 1);
977  $solHorizon = $quarterCircle - atan2($solHorizon, 1);
978 
979  if ($rise) {
980  $solHorizon = $fullCircle - $solHorizon;
981  }
982 
983  // time calculation
984  $localTime = $solHorizon + $solAscension - 0.0172028 * $radDay - 1.73364;
985  $universalTime = $localTime - $radLongitude;
986 
987  // determinate quadrant
988  $universalTime = $this->_range($universalTime, $fullCircle);
989 
990  // radiant to hours
991  $universalTime *= 24 / $fullCircle;
992 
993  // convert to time
994  $hour = intval($universalTime);
995  $universalTime = ($universalTime - $hour) * 60;
996  $min = intval($universalTime);
997  $universalTime = ($universalTime - $min) * 60;
998  $sec = intval($universalTime);
999 
1000  return $this->mktime($hour, $min, $sec, $this->date('m', $this->_unixTimestamp),
1001  $this->date('j', $this->_unixTimestamp), $this->date('Y', $this->_unixTimestamp),
1002  -1, true);
1003  }
1004 
1014  public function setTimezone($zone = null)
1015  {
1016  $oldzone = @date_default_timezone_get();
1017  if ($zone === null) {
1018  $zone = $oldzone;
1019  }
1020 
1021  // throw an error on false input, but only if the new date extension is available
1022  if (function_exists('timezone_open')) {
1023  if (!@timezone_open($zone)) {
1024  #require_once 'Zend/Date/Exception.php';
1025  throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", 0, null, $zone);
1026  }
1027  }
1028  // this can generate an error if the date extension is not available and a false timezone is given
1029  $result = @date_default_timezone_set($zone);
1030  if ($result === true) {
1031  $this->_offset = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
1032  $this->_timezone = $zone;
1033  }
1034  date_default_timezone_set($oldzone);
1035 
1036  if (($zone == 'UTC') or ($zone == 'GMT')) {
1037  $this->_dst = false;
1038  } else {
1039  $this->_dst = true;
1040  }
1041 
1042  return $this;
1043  }
1044 
1051  public function getTimezone()
1052  {
1053  return $this->_timezone;
1054  }
1055 
1063  public function getGmtOffset()
1064  {
1065  $date = $this->getDateParts($this->getUnixTimestamp(), true);
1066  $zone = @date_default_timezone_get();
1067  $result = @date_default_timezone_set($this->_timezone);
1068  if ($result === true) {
1069  $offset = $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
1070  $date['mon'], $date['mday'], $date['year'], false)
1071  - $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
1072  $date['mon'], $date['mday'], $date['year'], true);
1073  }
1074  date_default_timezone_set($zone);
1075 
1076  return $offset;
1077  }
1078 
1084  protected static function _getTagSupportForCache()
1085  {
1086  $backend = self::$_cache->getBackend();
1087  if ($backend instanceof Zend_Cache_Backend_ExtendedInterface) {
1088  $cacheOptions = $backend->getCapabilities();
1089  self::$_cacheTags = $cacheOptions['tags'];
1090  } else {
1091  self::$_cacheTags = false;
1092  }
1093 
1094  return self::$_cacheTags;
1095  }
1096 }
$old
Definition: website.php:27
calcSun($location, $horizon, $rise=false)
Definition: DateObject.php:913
date($format, $timestamp=null, $gmt=false)
Definition: DateObject.php:301
mktime($hour, $minute, $second, $month, $day, $year, $gmt=false)
Definition: DateObject.php:146
$id
Definition: fieldset.phtml:14
static _getTagSupportForCache()
setTimezone($zone=null)
setUnixTimestamp($timestamp=null)
Definition: DateObject.php:79
static isYearLeapYear($year)
Definition: DateObject.php:274
$format
Definition: list.phtml:12
getDateParts($timestamp=null, $fast=null)
Definition: DateObject.php:674
weekNumber($year, $month, $day)
Definition: DateObject.php:867
$i
Definition: gallery.phtml:31
static dayOfWeek($year, $month, $day)
Definition: DateObject.php:636