Home  >  Article  >  Backend Development  >  Implementation code of php time and date tool class

Implementation code of php time and date tool class

WBOY
WBOYOriginal
2016-07-25 08:56:041250browse
  1. /**
  2. * Function: php time and date tool class
  3. * Editor: bbs.it-home.org
  4. */
  5. DateTimeUtils::addDate('2013-12-01',1,'y');
  6. DateTimeUtils::getWeekDay('2013/10/01','/');
  7. DateTimeUtils::isLeapYear('2013');
  8. DateTimeUtils::timeFromNow(strtotime("2013-10-26 14:15:13"));
  9. class DateTimeUtils {
  10. /**
  11. * Checks for leap year, returns true if it is. No 2-digit year check. Also
  12. * handles julian calendar correctly.
  13. * @param integer $year year to check
  14. * @return boolean true if is leap year
  15. */
  16. public static function isLeapYear($year)
  17. {
  18. $year = self::digitCheck($year);
  19. if ($year % 4 != 0)
  20. return false;
  21. if ($year % 400 == 0)
  22. return true;
  23. // if gregorian calendar (>1582), century not-divisible by 400 is not leap
  24. else if ($year > 1582 && $year % 100 == 0)
  25. return false;
  26. return true;
  27. }
  28. /**
  29. * Fix 2-digit years. Works for any century.
  30. * Assumes that if 2-digit is more than 30 years in future, then previous century.
  31. * @param integer $y year
  32. * @return integer change two digit year into multiple digits
  33. */
  34. protected static function digitCheck($y)
  35. {
  36. if ($y < 100){
  37. $yr = (integer) date("Y");
  38. $century = (integer) ($yr /100);
  39. if ($yr%100 > 50) {
  40. $c1 = $century + 1;
  41. $c0 = $century;
  42. } else {
  43. $c1 = $century;
  44. $c0 = $century - 1;
  45. }
  46. $c1 *= 100;
  47. // if 2-digit year is less than 30 years in future, set it to this century
  48. // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  49. if (($y + $c1) < $yr+30) $y = $y + $c1;
  50. else $y = $y + $c0*100;
  51. }
  52. return $y;
  53. }
  54. /**
  55. * Returns 4-digit representation of the year.
  56. * @param integer $y year
  57. * @return integer 4-digit representation of the year
  58. */
  59. public static function get4DigitYear($y)
  60. {
  61. return self::digitCheck($y);
  62. }
  63. /**
  64. * Checks to see if the year, month, day are valid combination.
  65. * @param integer $y year
  66. * @param integer $m month
  67. * @param integer $d day
  68. * @return boolean true if valid date, semantic check only.
  69. */
  70. public static function isValidDate($y,$m,$d)
  71. {
  72. return checkdate($m, $d, $y);
  73. }
  74. public static function checkDate($date, $separator = "-") { //检查日期是否合法日期
  75. $dateArr = explode ($separator, $date);
  76. return self::isValidDate ($dateArr[0], $dateArr[1], $dateArr[2]);
  77. }
  78. /**
  79. * Checks to see if the hour, minute and second are valid.
  80. * @param integer $h hour
  81. * @param integer $m minute
  82. * @param integer $s second
  83. * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12.
  84. * @return boolean true if valid date, semantic check only.
  85. * @since 1.0.5
  86. */
  87. public static function isValidTime($h,$m,$s,$hs24=true)
  88. {
  89. if($hs24 && ($h < 0 || $h > 23) || !$hs24 && ($h < 1 || $h > 12)) return false;
  90. if($m > 59 || $m < 0) return false;
  91. if($s > 59 || $s < 0) return false;
  92. return true;
  93. }
  94. public static function checkTime($time, $separator = ":") { //检查时间是否合法时间
  95. $timeArr = explode($separator, $time);
  96. return self::isValidTime($timeArr[0], $timeArr[1],$timeArr[2]);
  97. }
  98. public static function addDate($date, $int, $unit = "d") { //日期的增加
  99. $value = array('y'=>'', 'm'=>'', 'd'=>'');
  100. $dateArr = explode ( "-", $date);
  101. if(array_key_exists($unit, $value)){
  102. $value[$unit] = $int;
  103. }else{
  104. return false;
  105. }
  106. return date ("Y-m-d", mktime (0, 0, 0, $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] +$value['y']));
  107. }
  108. public static function addDateTime($date, $int, $unit = "d") { //Add date
  109. $value = array('y'=>'', 'm'=>'', 'd'=>'', 'h'=>'', 'i'=>'');
  110. $dateArr = preg_split ( "/-|s|:/", $date);
  111. if (array_key_exists($unit, $value)){
  112. $value[$unit] = $int;
  113. }else{
  114. return false;
  115. }
  116. return date ("Y-m-d H:i:s", mktime($dateArr[ 3]+ $value['h'], $dateArr[4]+ $value['i'], $dateArr[5], $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] + $value['y']));
  117. }
  118. public static function addDayTimestamp($ntime, $aday) { //Get the days after the current time, The increasing unit of days is 1
  119. $dayst = 3600 * 24;
  120. $oktime = $ntime + ($aday * $dayst);
  121. return $oktime;
  122. }
  123. public static function dateDiff($begin, $end, $unit = "d") { //Time comparison function, returns the difference in seconds, minutes, hours or days between two dates
  124. $diff = strtotime($end) - strtotime($begin);
  125. switch($unit)
  126. {
  127. case "y": $retval = bcdiv($diff, (60 * 60 * 24 * 365)); break;
  128. case "m": $retval = bcdiv($diff, (60 * 60 * 24 * 30)); break;
  129. case "w": $retval = bcdiv($diff, (60 * 60 * 24 * 7)); break;
  130. case "d": $retval = bcdiv($diff, (60 * 60 * 24)); break;
  131. case "h": $retval = bcdiv($diff, (60 * 60)); break;
  132. case "i": $retval = bcdiv($diff, 60); break;
  133. case "s": $retval = $diff; break;
  134. }
  135. return $retval;
  136. }
  137. public static function getWeekDay($date, $separator = "-") { //Calculate the given date is Day of the week
  138. $dateArr = explode ($separator, $date);
  139. return date ("w", mktime (0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0])) ;
  140. }
  141. public static function timeFromNow($dateline) { //Let the date be displayed as: XX days XX years ago
  142. if(empty($dateline)) return false;
  143. $seconds = time() - $dateline;
  144. if($seconds < 60){
  145. return "1 minute ago";
  146. }elseif($seconds < 3600){
  147. return floor($seconds/60)."minutes ago";
  148. }elseif($seconds < ; 24*3600){
  149. return floor($seconds/3600)."hours ago";
  150. }elseif($seconds < 48*3600){
  151. return date("Yesterday H:i", $dateline)." ";
  152. }else{
  153. return date('Y-m-d', $dateline);
  154. }
  155. }
  156. public static function transDateToChs($date) {
  157. if (empty ($date)) return 'today';
  158. date_default_timezone_set('PRC');
  159. $dates = date ('Y year m month d day', strtotime ($date));
  160. return $dates;
  161. }
  162. // 08/31/2004 => 2004- 08-31
  163. public static function TransDateUI($datestr, $type = 'Y-m-d') {
  164. if ($datestr == Null)
  165. return Null;
  166. $target = $datestr;
  167. $arr_date = preg_split ( "/ //", $target);
  168. $monthstr = $arr_date[0];
  169. $daystr = $arr_date[1];
  170. $yearstr = $arr_date[2];
  171. $result = date ($type, mktime (0 , 0, 0, $monthstr, $daystr, $yearstr));
  172. return $result;
  173. }
  174. // 12/20/2004 10:55 AM => 2004-12-20 10:55:00
  175. public static function TransDateTimeUI($datestr, $type = 'Y-m-d H:i:s') {
  176. if ($datestr == Null)
  177. return Null;
  178. $target = $datestr;
  179. $arr_date = preg_split ( "// |s|:/", $target);
  180. $monthstr = $arr_date[0];
  181. $daystr = $arr_date[1];
  182. $yearstr = $arr_date[2];
  183. $hourstr = $arr_date[3] ;
  184. $minutesstr = $arr_date[4];
  185. $result = date ($type, mktime ($hourstr, $minutesstr, 0, $monthstr, $daystr, $yearstr));
  186. return $result;
  187. }
  188. }
  189. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn