PageRenderTime 66ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/utils/CTimestamp.php

https://gitlab.com/zenfork/vektor
PHP | 376 lines | 216 code | 34 blank | 126 comment | 77 complexity | e96cb7c0412e7a74812bc2e37f14c5eb MD5 | raw file
  1. <?php
  2. /**
  3. * CTimestamp class file.
  4. *
  5. * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CTimestamp represents a timestamp.
  12. *
  13. * Part of this class was adapted from the ADOdb Date Library
  14. * {@link http://phplens.com/phpeverywhere/ ADOdb abstraction library}.
  15. * The original source code was released under both BSD and GNU Lesser GPL
  16. * library license, with the following copyright notice:
  17. * Copyright (c) 2000, 2001, 2002, 2003, 2004 John Lim
  18. * All rights reserved.
  19. *
  20. * This class is provided to support UNIX timestamp that is beyond the range
  21. * of 1901-2038 on Unix and1970-2038 on Windows. Except {@link getTimestamp},
  22. * all other methods in this class can work with the extended timestamp range.
  23. * For {@link getTimestamp}, because it is merely a wrapper of
  24. * {@link mktime http://php.net/manual/en/function.mktime.php}, it may still
  25. * be subject to the limit of timestamp range on certain platforms. Please refer
  26. * to the PHP manual for more information.
  27. *
  28. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  29. * @package system.utils
  30. * @since 1.0
  31. */
  32. class CTimestamp
  33. {
  34. /**
  35. * Gets day of week, 0 = Sunday,... 6=Saturday.
  36. * Algorithm from PEAR::Date_Calc
  37. * @param integer $year year
  38. * @param integer $month month
  39. * @param integer $day day
  40. * @return integer day of week
  41. */
  42. public static function getDayofWeek($year, $month, $day)
  43. {
  44. /*
  45. Pope Gregory removed 10 days - October 5 to October 14 - from the year 1582 and
  46. proclaimed that from that time onwards 3 days would be dropped from the calendar
  47. every 400 years.
  48. Thursday, October 4, 1582 (Julian) was followed immediately by Friday, October 15, 1582 (Gregorian).
  49. */
  50. if ($year <= 1582)
  51. {
  52. if ($year < 1582 ||
  53. ($year == 1582 && ($month < 10 || ($month == 10 && $day < 15))))
  54. {
  55. $greg_correction = 3;
  56. }
  57. else
  58. {
  59. $greg_correction = 0;
  60. }
  61. }
  62. else
  63. {
  64. $greg_correction = 0;
  65. }
  66. if($month > 2)
  67. $month -= 2;
  68. else
  69. {
  70. $month += 10;
  71. $year--;
  72. }
  73. $day = floor((13 * $month - 1) / 5) +
  74. $day + ($year % 100) +
  75. floor(($year % 100) / 4) +
  76. floor(($year / 100) / 4) - 2 *
  77. floor($year / 100) + 77 + $greg_correction;
  78. return $day - 7 * floor($day / 7);
  79. }
  80. /**
  81. * Checks for leap year, returns true if it is. No 2-digit year check. Also
  82. * handles julian calendar correctly.
  83. * @param integer $year year to check
  84. * @return boolean true if is leap year
  85. */
  86. public static function isLeapYear($year)
  87. {
  88. $year = self::digitCheck($year);
  89. if ($year % 4 != 0)
  90. return false;
  91. if ($year % 400 == 0)
  92. return true;
  93. // if gregorian calendar (>1582), century not-divisible by 400 is not leap
  94. elseif ($year > 1582 && $year % 100 == 0 )
  95. return false;
  96. return true;
  97. }
  98. /**
  99. * Fix 2-digit years. Works for any century.
  100. * Assumes that if 2-digit is more than 30 years in future, then previous century.
  101. * @param integer $y year
  102. * @return integer change two digit year into multiple digits
  103. */
  104. protected static function digitCheck($y)
  105. {
  106. if ($y < 100){
  107. $yr = (integer) date("Y");
  108. $century = (integer) ($yr /100);
  109. if ($yr%100 > 50) {
  110. $c1 = $century + 1;
  111. $c0 = $century;
  112. } else {
  113. $c1 = $century;
  114. $c0 = $century - 1;
  115. }
  116. $c1 *= 100;
  117. // if 2-digit year is less than 30 years in future, set it to this century
  118. // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  119. if (($y + $c1) < $yr+30) $y = $y + $c1;
  120. else $y = $y + $c0*100;
  121. }
  122. return $y;
  123. }
  124. /**
  125. * Returns 4-digit representation of the year.
  126. * @param integer $y year
  127. * @return integer 4-digit representation of the year
  128. */
  129. public static function get4DigitYear($y)
  130. {
  131. return self::digitCheck($y);
  132. }
  133. /**
  134. * @return integer get local time zone offset from GMT
  135. */
  136. public static function getGMTDiff()
  137. {
  138. static $TZ;
  139. if (isset($TZ)) return $TZ;
  140. $TZ = mktime(0,0,0,1,2,1970) - gmmktime(0,0,0,1,2,1970);
  141. return $TZ;
  142. }
  143. /**
  144. * Returns the getdate() array.
  145. * @param integer|boolean $d original date timestamp. False to use the current timestamp.
  146. * @param boolean $fast false to compute the day of the week, default is true
  147. * @param boolean $gmt true to calculate the GMT dates
  148. * @return array an array with date info.
  149. */
  150. public static function getDate($d=false,$fast=false,$gmt=false)
  151. {
  152. if($d===false)
  153. $d=time();
  154. if($gmt)
  155. {
  156. $tz = date_default_timezone_get();
  157. date_default_timezone_set('GMT');
  158. $result = getdate($d);
  159. date_default_timezone_set($tz);
  160. }
  161. else
  162. {
  163. $result = getdate($d);
  164. }
  165. return $result;
  166. }
  167. /**
  168. * Checks to see if the year, month, day are valid combination.
  169. * @param integer $y year
  170. * @param integer $m month
  171. * @param integer $d day
  172. * @return boolean true if valid date, semantic check only.
  173. */
  174. public static function isValidDate($y,$m,$d)
  175. {
  176. return checkdate($m, $d, $y);
  177. }
  178. /**
  179. * Checks to see if the hour, minute and second are valid.
  180. * @param integer $h hour
  181. * @param integer $m minute
  182. * @param integer $s second
  183. * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12.
  184. * @return boolean true if valid date, semantic check only.
  185. */
  186. public static function isValidTime($h,$m,$s,$hs24=true)
  187. {
  188. if($hs24 && ($h < 0 || $h > 23) || !$hs24 && ($h < 1 || $h > 12)) return false;
  189. if($m > 59 || $m < 0) return false;
  190. if($s > 59 || $s < 0) return false;
  191. return true;
  192. }
  193. /**
  194. * Formats a timestamp to a date string.
  195. * @param string $fmt format pattern
  196. * @param integer|boolean $d timestamp
  197. * @param boolean $is_gmt whether this is a GMT timestamp
  198. * @return string formatted date based on timestamp $d
  199. */
  200. public static function formatDate($fmt,$d=false,$is_gmt=false)
  201. {
  202. if ($d === false)
  203. return ($is_gmt)? @gmdate($fmt): @date($fmt);
  204. // check if number in 32-bit signed range
  205. if ((abs($d) <= 0x7FFFFFFF))
  206. {
  207. // if windows, must be +ve integer
  208. if ($d >= 0)
  209. return ($is_gmt)? @gmdate($fmt,$d): @date($fmt,$d);
  210. }
  211. $_day_power = 86400;
  212. $arr = self::getDate($d,true,$is_gmt);
  213. $year = $arr['year'];
  214. $month = $arr['mon'];
  215. $day = $arr['mday'];
  216. $hour = $arr['hours'];
  217. $min = $arr['minutes'];
  218. $secs = $arr['seconds'];
  219. $max = strlen($fmt);
  220. $dates = '';
  221. /*
  222. at this point, we have the following integer vars to manipulate:
  223. $year, $month, $day, $hour, $min, $secs
  224. */
  225. for ($i=0; $i < $max; $i++)
  226. {
  227. switch($fmt[$i])
  228. {
  229. case 'T': $dates .= date('T');break;
  230. // YEAR
  231. case 'L': $dates .= $arr['leap'] ? '1' : '0'; break;
  232. case 'r': // Thu, 21 Dec 2000 16:01:07 +0200
  233. // 4.3.11 uses '04 Jun 2004'
  234. // 4.3.8 uses ' 4 Jun 2004'
  235. $dates .= gmdate('D',$_day_power*(3+self::getDayOfWeek($year,$month,$day))).', '
  236. . ($day<10?'0'.$day:$day) . ' '.date('M',mktime(0,0,0,$month,2,1971)).' '.$year.' ';
  237. if ($hour < 10) $dates .= '0'.$hour; else $dates .= $hour;
  238. if ($min < 10) $dates .= ':0'.$min; else $dates .= ':'.$min;
  239. if ($secs < 10) $dates .= ':0'.$secs; else $dates .= ':'.$secs;
  240. $gmt = self::getGMTDiff();
  241. $dates .= sprintf(' %s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
  242. break;
  243. case 'Y': $dates .= $year; break;
  244. case 'y': $dates .= substr($year,strlen($year)-2,2); break;
  245. // MONTH
  246. case 'm': if ($month<10) $dates .= '0'.$month; else $dates .= $month; break;
  247. case 'Q': $dates .= ($month+3)>>2; break;
  248. case 'n': $dates .= $month; break;
  249. case 'M': $dates .= date('M',mktime(0,0,0,$month,2,1971)); break;
  250. case 'F': $dates .= date('F',mktime(0,0,0,$month,2,1971)); break;
  251. // DAY
  252. case 't': $dates .= $arr['ndays']; break;
  253. case 'z': $dates .= $arr['yday']; break;
  254. case 'w': $dates .= self::getDayOfWeek($year,$month,$day); break;
  255. case 'l': $dates .= gmdate('l',$_day_power*(3+self::getDayOfWeek($year,$month,$day))); break;
  256. case 'D': $dates .= gmdate('D',$_day_power*(3+self::getDayOfWeek($year,$month,$day))); break;
  257. case 'j': $dates .= $day; break;
  258. case 'd': if ($day<10) $dates .= '0'.$day; else $dates .= $day; break;
  259. case 'S':
  260. $d10 = $day % 10;
  261. if ($d10 == 1) $dates .= 'st';
  262. elseif ($d10 == 2 && $day != 12) $dates .= 'nd';
  263. elseif ($d10 == 3) $dates .= 'rd';
  264. else $dates .= 'th';
  265. break;
  266. // HOUR
  267. case 'Z':
  268. $dates .= ($is_gmt) ? 0 : -self::getGMTDiff(); break;
  269. case 'O':
  270. $gmt = ($is_gmt) ? 0 : self::getGMTDiff();
  271. $dates .= sprintf('%s%04d',($gmt<=0)?'+':'-',abs($gmt)/36);
  272. break;
  273. case 'H':
  274. if ($hour < 10) $dates .= '0'.$hour;
  275. else $dates .= $hour;
  276. break;
  277. case 'h':
  278. if ($hour > 12) $hh = $hour - 12;
  279. else {
  280. if ($hour == 0) $hh = '12';
  281. else $hh = $hour;
  282. }
  283. if ($hh < 10) $dates .= '0'.$hh;
  284. else $dates .= $hh;
  285. break;
  286. case 'G':
  287. $dates .= $hour;
  288. break;
  289. case 'g':
  290. if ($hour > 12) $hh = $hour - 12;
  291. else {
  292. if ($hour == 0) $hh = '12';
  293. else $hh = $hour;
  294. }
  295. $dates .= $hh;
  296. break;
  297. // MINUTES
  298. case 'i': if ($min < 10) $dates .= '0'.$min; else $dates .= $min; break;
  299. // SECONDS
  300. case 'U': $dates .= $d; break;
  301. case 's': if ($secs < 10) $dates .= '0'.$secs; else $dates .= $secs; break;
  302. // AM/PM
  303. // Note 00:00 to 11:59 is AM, while 12:00 to 23:59 is PM
  304. case 'a':
  305. if ($hour>=12) $dates .= 'pm';
  306. else $dates .= 'am';
  307. break;
  308. case 'A':
  309. if ($hour>=12) $dates .= 'PM';
  310. else $dates .= 'AM';
  311. break;
  312. default:
  313. $dates .= $fmt[$i]; break;
  314. // ESCAPE
  315. case "\\":
  316. $i++;
  317. if ($i < $max) $dates .= $fmt[$i];
  318. break;
  319. }
  320. }
  321. return $dates;
  322. }
  323. /**
  324. * Generates a timestamp.
  325. * This is the same as the PHP function {@link mktime http://php.net/manual/en/function.mktime.php}.
  326. * @param integer $hr hour
  327. * @param integer $min minute
  328. * @param integer $sec second
  329. * @param integer|boolean $mon month
  330. * @param integer|boolean $day day
  331. * @param integer|boolean $year year
  332. * @param boolean $is_gmt whether this is GMT time. If true, gmmktime() will be used.
  333. * @return integer|float a timestamp given a local time.
  334. */
  335. public static function getTimestamp($hr,$min,$sec,$mon=false,$day=false,$year=false,$is_gmt=false)
  336. {
  337. if ($mon === false)
  338. return $is_gmt? @gmmktime($hr,$min,$sec): @mktime($hr,$min,$sec);
  339. return $is_gmt ? @gmmktime($hr,$min,$sec,$mon,$day,$year) : @mktime($hr,$min,$sec,$mon,$day,$year);
  340. }
  341. }