php时间日期工具类

jerry PHP 2015年11月19日 收藏
  1. <?php
  2. DateTimeUtils::addDate('2012-12-01',1,'y');
  3. DateTimeUtils::getWeekDay('2012/10/01','/');
  4. DateTimeUtils::isLeapYear('2012');
  5. DateTimeUtils::timeFromNow(strtotime("2012-10-26 14:15:13"));
  6. class DateTimeUtils {
  7. /**
  8.  * Checks for leap year, returns true if it is. No 2-digit year check. Also
  9.  * handles julian calendar correctly.
  10.  * @param integer $year year to check
  11.  * @return boolean true if is leap year
  12.  */
  13. public static function isLeapYear($year)
  14. {
  15. $year = self::digitCheck($year);
  16. if ($year % 4 != 0)
  17. return false;
  18.  
  19. if ($year % 400 == 0)
  20. return true;
  21. // if gregorian calendar (>1582), century not-divisible by 400 is not leap
  22. else if ($year > 1582 && $year % 100 == 0)
  23. return false;
  24. return true;
  25. }
  26.  
  27. /**
  28.  * Fix 2-digit years. Works for any century.
  29.  * Assumes that if 2-digit is more than 30 years in future, then previous century.
  30.  * @param integer $y year
  31.  * @return integer change two digit year into multiple digits
  32.  */
  33. protected static function digitCheck($y)
  34. {
  35. if ($y < 100){
  36. $yr = (integer) date("Y");
  37. $century = (integer) ($yr /100);
  38.  
  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. /**
  56.  * Returns 4-digit representation of the year.
  57.  * @param integer $y year
  58.  * @return integer 4-digit representation of the year
  59.  */
  60. public static function get4DigitYear($y)
  61. {
  62. return self::digitCheck($y);
  63. }
  64. /**
  65.  * Checks to see if the year, month, day are valid combination.
  66.  * @param integer $y year
  67.  * @param integer $m month
  68.  * @param integer $d day
  69.  * @return boolean true if valid date, semantic check only.
  70.  */
  71. public static function isValidDate($y,$m,$d)
  72. {
  73. return checkdate($m, $d, $y);
  74. }
  75.  
  76. public static function checkDate($date, $separator = "-") { //检查日期是否合法日期
  77. $dateArr = explode ($separator, $date);
  78. return self::isValidDate ($dateArr[0], $dateArr[1], $dateArr[2]);
  79. }
  80. /**
  81.  * Checks to see if the hour, minute and second are valid.
  82.  * @param integer $h hour
  83.  * @param integer $m minute
  84.  * @param integer $s second
  85.  * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12.
  86.  * @return boolean true if valid date, semantic check only.
  87.  * @since 1.0.5
  88.  */
  89. public static function isValidTime($h,$m,$s,$hs24=true)
  90. {
  91. if($hs24 && ($h < 0 || $h > 23) || !$hs24 && ($h < 1 || $h > 12)) return false;
  92. if($m > 59 || $m < 0) return false;
  93. if($s > 59 || $s < 0) return false;
  94. return true;
  95. }
  96.  
  97. public static function checkTime($time, $separator = ":") { //检查时间是否合法时间
  98. $timeArr = explode($separator, $time);
  99. return self::isValidTime($timeArr[0], $timeArr[1],$timeArr[2]);
  100. }
  101.  
  102. public static function addDate($date, $int, $unit = "d") { //日期的增加
  103. $value = array('y'=>'', 'm'=>'', 'd'=>'');
  104. $dateArr = explode ( "-", $date);
  105. if(array_key_exists($unit, $value)){
  106. $value[$unit] = $int;
  107. }else{
  108. return false;
  109. }
  110. return date ("Y-m-d", mktime (0, 0, 0, $dateArr[1] + $value['m'], $dateArr[2] + $value['d'], $dateArr[0] +$value['y']));
  111. }
  112.  
  113. public static function addDateTime($date, $int, $unit = "d") { //日期的增加
  114. $value = array('y'=>'', 'm'=>'', 'd'=>'', 'h'=>'', 'i'=>'');
  115. $dateArr = preg_split ( "/-|\s|:/", $date);
  116. if(array_key_exists($unit, $value)){
  117. $value[$unit] = $int;
  118. }else{
  119. return false;
  120. }
  121. 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']));
  122. }
  123.  
  124. public static function addDayTimestamp($ntime, $aday) { //取当前时间后几天,天数增加单位为1
  125. $dayst = 3600 * 24;
  126. $oktime = $ntime + ($aday * $dayst);
  127. return $oktime;
  128. }
  129.  
  130. public static function dateDiff($begin, $end, $unit = "d") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
  131. $diff = strtotime($end) - strtotime($begin);
  132. switch($unit)
  133. {
  134. case "y": $retval = bcdiv($diff, (60 * 60 * 24 * 365)); break;
  135. case "m": $retval = bcdiv($diff, (60 * 60 * 24 * 30)); break;
  136. case "w": $retval = bcdiv($diff, (60 * 60 * 24 * 7)); break;
  137. case "d": $retval = bcdiv($diff, (60 * 60 * 24)); break;
  138. case "h": $retval = bcdiv($diff, (60 * 60)); break;
  139. case "i": $retval = bcdiv($diff, 60); break;
  140. case "s": $retval = $diff; break;
  141. }
  142. return $retval;
  143. }
  144.  
  145. public static function getWeekDay($date, $separator = "-") { //计算出给出的日期是星期几
  146. $dateArr = explode ($separator, $date);
  147. return date ("w", mktime ( 0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0]));
  148. }
  149.  
  150. public static function timeFromNow($dateline) { //让日期显示为:XX天XX年以前
  151. if(empty($dateline)) return false;
  152. $seconds = time() - $dateline;
  153. if($seconds < 60){
  154. return "1分钟前";
  155. }elseif($seconds < 3600){
  156. return floor($seconds/60)."分钟前";
  157. }elseif($seconds  < 24*3600){
  158. return floor($seconds/3600)."小时前";
  159. }elseif($seconds < 48*3600){
  160. return date("昨天 H:i", $dateline)."";
  161. }else{
  162. return date('Y-m-d', $dateline);
  163. }
  164. }
  165.  
  166. public static function transDateToChs($date) {
  167. if (empty ($date)) return '今日';
  168. date_default_timezone_set('PRC');
  169. $dates = date ('Y年m月d日', strtotime ($date));
  170. return $dates;
  171. }
  172.  
  173. // 08/31/2004 => 2004-08-31
  174. public static function TransDateUI($datestr, $type = 'Y-m-d') {
  175. if ($datestr == Null)
  176. return Null;
  177. $target = $datestr;
  178. $arr_date = preg_split ( "/\//", $target);
  179. $monthstr = $arr_date[0];
  180. $daystr = $arr_date[1];
  181. $yearstr = $arr_date[2];
  182. $result = date ($type, mktime (0, 0, 0, $monthstr, $daystr, $yearstr));
  183. return $result;
  184. }
  185.  
  186. // 12/20/2004 10:55 AM => 2004-12-20 10:55:00
  187. public static function TransDateTimeUI($datestr, $type = 'Y-m-d H:i:s') {
  188. if ($datestr == Null)
  189. return Null;
  190. $target = $datestr;
  191. $arr_date = preg_split ( "/\/|\s|:/", $target);
  192. $monthstr = $arr_date[0];
  193. $daystr = $arr_date[1];
  194. $yearstr = $arr_date[2];
  195. $hourstr = $arr_date[3];
  196. $minutesstr = $arr_date[4];
  197. $result = date ($type, mktime ($hourstr, $minutesstr, 0, $monthstr, $daystr, $yearstr));
  198. return $result;
  199. }
  200. }
  201. ?>