Heim  >  Artikel  >  Backend-Entwicklung  >  php时间日期工具类的实现代码

php时间日期工具类的实现代码

WBOY
WBOYOriginal
2016-07-25 08:56:041250Durchsuche
  1. /**
  2. * 功能:php时间与日期工具类
  3. * 编辑: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 $yr = (integer) date("Y");
  37. $century = (integer) ($yr /100);
  38. if ($yr%100 > 50) {
  39. $c1 = $century + 1;
  40. $c0 = $century;
  41. } else {
  42. $c1 = $century;
  43. $c0 = $century - 1;
  44. }
  45. $c1 *= 100;
  46. // if 2-digit year is less than 30 years in future, set it to this century
  47. // otherwise if more than 30 years in future, then we set 2-digit year to the prev century.
  48. if (($y + $c1) else $y = $y + $c0*100;
  49. }
  50. return $y;
  51. }
  52. /**
  53. * Returns 4-digit representation of the year.
  54. * @param integer $y year
  55. * @return integer 4-digit representation of the year
  56. */
  57. public static function get4DigitYear($y)
  58. {
  59. return self::digitCheck($y);
  60. }
  61. /**
  62. * Checks to see if the year, month, day are valid combination.
  63. * @param integer $y year
  64. * @param integer $m month
  65. * @param integer $d day
  66. * @return boolean true if valid date, semantic check only.
  67. */
  68. public static function isValidDate($y,$m,$d)
  69. {
  70. return checkdate($m, $d, $y);
  71. }
  72. public static function checkDate($date, $separator = "-") { //检查日期是否合法日期
  73. $dateArr = explode ($separator, $date);
  74. return self::isValidDate ($dateArr[0], $dateArr[1], $dateArr[2]);
  75. }
  76. /**
  77. * Checks to see if the hour, minute and second are valid.
  78. * @param integer $h hour
  79. * @param integer $m minute
  80. * @param integer $s second
  81. * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12.
  82. * @return boolean true if valid date, semantic check only.
  83. * @since 1.0.5
  84. */
  85. public static function isValidTime($h,$m,$s,$hs24=true)
  86. {
  87. if($hs24 && ($h 23) || !$hs24 && ($h 12)) return false;
  88. if($m > 59 || $m if($s > 59 || $s return true;
  89. }
  90. public static function checkTime($time, $separator = ":") { //检查时间是否合法时间
  91. $timeArr = explode($separator, $time);
  92. return self::isValidTime($timeArr[0], $timeArr[1],$timeArr[2]);
  93. }
  94. public static function addDate($date, $int, $unit = "d") { //日期的增加
  95. $value = array('y'=>'', 'm'=>'', 'd'=>'');
  96. $dateArr = explode ( "-", $date);
  97. if(array_key_exists($unit, $value)){
  98. $value[$unit] = $int;
  99. }else{
  100. return false;
  101. }
  102. return date ("Y-m-d", mktime (0, 0, 0, $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] +$value['y']));
  103. }
  104. public static function addDateTime($date, $int, $unit = "d") { //日期的增加
  105. $value = array('y'=>'', 'm'=>'', 'd'=>'', 'h'=>'', 'i'=>'');
  106. $dateArr = preg_split ( "/-|\s|:/", $date);
  107. if(array_key_exists($unit, $value)){
  108. $value[$unit] = $int;
  109. }else{
  110. return false;
  111. }
  112. 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']));
  113. }
  114. public static function addDayTimestamp($ntime, $aday) { //取当前时间后几天,天数增加单位为1
  115. $dayst = 3600 * 24;
  116. $oktime = $ntime + ($aday * $dayst);
  117. return $oktime;
  118. }
  119. public static function dateDiff($begin, $end, $unit = "d") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
  120. $diff = strtotime($end) - strtotime($begin);
  121. switch($unit)
  122. {
  123. case "y": $retval = bcdiv($diff, (60 * 60 * 24 * 365)); break;
  124. case "m": $retval = bcdiv($diff, (60 * 60 * 24 * 30)); break;
  125. case "w": $retval = bcdiv($diff, (60 * 60 * 24 * 7)); break;
  126. case "d": $retval = bcdiv($diff, (60 * 60 * 24)); break;
  127. case "h": $retval = bcdiv($diff, (60 * 60)); break;
  128. case "i": $retval = bcdiv($diff, 60); break;
  129. case "s": $retval = $diff; break;
  130. }
  131. return $retval;
  132. }
  133. public static function getWeekDay($date, $separator = "-") { //计算出给出的日期是星期几
  134. $dateArr = explode ($separator, $date);
  135. return date ("w", mktime ( 0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0]));
  136. }
  137. public static function timeFromNow($dateline) { //让日期显示为:XX天XX年以前
  138. if(empty($dateline)) return false;
  139. $seconds = time() - $dateline;
  140. if($seconds return "1分钟前";
  141. }elseif($seconds return floor($seconds/60)."分钟前";
  142. }elseif($seconds return floor($seconds/3600)."小时前";
  143. }elseif($seconds return date("昨天 H:i", $dateline)."";
  144. }else{
  145. return date('Y-m-d', $dateline);
  146. }
  147. }
  148. public static function transDateToChs($date) {
  149. if (empty ($date)) return '今日';
  150. date_default_timezone_set('PRC');
  151. $dates = date ('Y年m月d日', strtotime ($date));
  152. return $dates;
  153. }
  154. // 08/31/2004 => 2004-08-31
  155. public static function TransDateUI($datestr, $type = 'Y-m-d') {
  156. if ($datestr == Null)
  157. return Null;
  158. $target = $datestr;
  159. $arr_date = preg_split ( "/\//", $target);
  160. $monthstr = $arr_date[0];
  161. $daystr = $arr_date[1];
  162. $yearstr = $arr_date[2];
  163. $result = date ($type, mktime (0, 0, 0, $monthstr, $daystr, $yearstr));
  164. return $result;
  165. }
  166. // 12/20/2004 10:55 AM => 2004-12-20 10:55:00
  167. public static function TransDateTimeUI($datestr, $type = 'Y-m-d H:i:s') {
  168. if ($datestr == Null)
  169. return Null;
  170. $target = $datestr;
  171. $arr_date = preg_split ( "/\/|\s|:/", $target);
  172. $monthstr = $arr_date[0];
  173. $daystr = $arr_date[1];
  174. $yearstr = $arr_date[2];
  175. $hourstr = $arr_date[3];
  176. $minutesstr = $arr_date[4];
  177. $result = date ($type, mktime ($hourstr, $minutesstr, 0, $monthstr, $daystr, $yearstr));
  178. return $result;
  179. }
  180. }
  181. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn