3 * TDateTimeStamp class file.
5 * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
13 * TDateTimeStamp Class
15 * TDateTimeStamp Class is a wrapper to DateTime: http://php.net/manual/book.datetime.php
16 * Prado implemented this class before php started shipping the DateTime extension.
17 * This class is deprecated and left here only for compatibility for legacy code.
18 * Please note that this class doesn't support automatic conversion from gregorian to
19 * julian dates anymore.
21 * @author Fabio Bas ctrlaltca[AT]gmail[DOT]com
22 * @package System.Util
24 * @deprecated since 3.2.1
28 protected static $_month_normal = array("",31,28,31,30,31,30,31,31,30,31,30,31);
29 protected static $_month_leaf = array("",31,29,31,30,31,30,31,31,30,31,30,31);
32 * Returns the day of the week (0=Sunday, 1=Monday, .. 6=Saturday)
37 public function getDayofWeek($year, $month, $day)
40 $dt->setDate($year, $month, $day);
41 return (int) $dt->format('w');
45 * Checks for leap year, returns true if it is. No 2-digit year check. Also
46 * handles julian calendar correctly.
47 * @param float year to check
48 * @return boolean true if is leap year
50 public function isLeapYear($year)
52 $year = $this->digitCheck($year);
54 $dt->setDate($year, 1, 1);
55 return (bool) $dt->format('L');
59 * Fix 2-digit years. Works for any century.
60 * Assumes that if 2-digit is more than 30 years in future, then previous century.
61 * @return integer change two digit year into multiple digits
63 protected function digitCheck($y)
66 $yr = (integer) date("Y");
67 $century = (integer) ($yr /100);
77 // if 2-digit year is less than 30 years in future, set it to this century
78 // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
79 if (($y + $c1) < $yr+30) $y = $y + $c1;
80 else $y = $y + $c0*100;
85 public function get4DigitYear($y)
87 return $this->digitCheck($y);
91 * @return integer get local time zone offset from GMT
93 public function getGMTDiff($ts=false)
97 $dt->setTimeStamp($ts);
99 $dt->setDate(1970, 1, 2);
101 return (int) $dt->format('Z');
105 * @return array an array with date info.
107 function parseDate($txt=false)
109 if ($txt === false) return getdate();
111 $dt = new DateTime($txt);
114 'seconds' => (int) $dt->format('s'),
115 'minutes' => (int) $dt->format('i'),
116 'hours' => (int) $dt->format('G'),
117 'mday' => (int) $dt->format('j'),
118 'wday' => (int) $dt->format('w'),
119 'mon' => (int) $dt->format('n'),
120 'year' => (int) $dt->format('Y'),
121 'yday' => (int) $dt->format('z'),
122 'weekday' => $dt->format('l'),
123 'month' => $dt->format('F'),
124 0 => (int) $dt->format('U'),
129 * @return array an array with date info.
131 function getDate($d=false,$fast=false)
133 if ($d === false) return getdate();
135 $dt = new DateTime();
136 $dt->setTimestamp($d);
139 'seconds' => (int) $dt->format('s'),
140 'minutes' => (int) $dt->format('i'),
141 'hours' => (int) $dt->format('G'),
142 'mday' => (int) $dt->format('j'),
143 'wday' => (int) $dt->format('w'),
144 'mon' => (int) $dt->format('n'),
145 'year' => (int) $dt->format('Y'),
146 'yday' => (int) $dt->format('z'),
147 'weekday' => $dt->format('l'),
148 'month' => $dt->format('F'),
149 0 => (int) $dt->format('U'),
154 * @return boolean true if valid date, semantic check only.
156 public function isValidDate($y,$m,$d)
158 if ($this->isLeapYear($y))
159 $marr =& self::$_month_leaf;
161 $marr =& self::$_month_normal;
163 if ($m > 12 || $m < 1) return false;
165 if ($d > 31 || $d < 1) return false;
167 if ($marr[$m] < $d) return false;
169 if ($y < 1000 && $y > 3000) return false;
175 * @return string formatted date based on timestamp $d
177 function formatDate($fmt,$ts=false,$is_gmt=false)
179 $dt = new DateTime();
181 $dt->setTimeZone(new DateTimeZone('UTC'));
182 $dt->setTimestamp($ts);
184 return $dt->format($fmt);
188 * @return integer|float a timestamp given a local time
190 function getTimeStamp($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_gmt=false)
192 $dt = new DateTime();
194 $dt->setTimeZone(new DateTimeZone('UTC'));
195 $dt->setDate($year!==false ? $year : date('Y'),
196 $mon!==false ? $mon : date('m'),
197 $day!==false ? $day : date('d'));
198 $dt->setTime($hr, $min, $sec);
199 return (int) $dt->format('U');