您好,欢迎访问一九零五行业门户网

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

1582), century not-divisible by 400 is not leap else if ($year > 1582 && $year % 100 == 0) return false; return true; } /** * fix 2-digit years. works for any century. * assumes that if 2-digit is more than 30 years in future, then previous century. * @param integer $y year * @return integer change two digit year into multiple digits */ protected static function digitcheck($y) { if ($y $yr = (integer) date(y); $century = (integer) ($yr /100); if ($yr%100 > 50) { $c1 = $century + 1; $c0 = $century; } else { $c1 = $century; $c0 = $century - 1; } $c1 *= 100; // if 2-digit year is less than 30 years in future, set it to this century // otherwise if more than 30 years in future, then we set 2-digit year to the prev century. if (($y + $c1) else $y = $y + $c0*100; } return $y; } /** * returns 4-digit representation of the year. * @param integer $y year * @return integer 4-digit representation of the year */ public static function get4digityear($y) { return self::digitcheck($y); } /** * checks to see if the year, month, day are valid combination. * @param integer $y year * @param integer $m month * @param integer $d day * @return boolean true if valid date, semantic check only. */ public static function isvaliddate($y,$m,$d) { return checkdate($m, $d, $y); } public static function checkdate($date, $separator = -) { //检查日期是否合法日期 $datearr = explode ($separator, $date); return self::isvaliddate ($datearr[0], $datearr[1], $datearr[2]); } /** * checks to see if the hour, minute and second are valid. * @param integer $h hour * @param integer $m minute * @param integer $s second * @param boolean $hs24 whether the hours should be 0 through 23 (default) or 1 through 12. * @return boolean true if valid date, semantic check only. * @since 1.0.5 */ public static function isvalidtime($h,$m,$s,$hs24=true) { if($hs24 && ($h 23) || !$hs24 && ($h 12)) return false; if($m > 59 || $m if($s > 59 || $s return true; } public static function checktime($time, $separator = :) { //检查时间是否合法时间 $timearr = explode($separator, $time); return self::isvalidtime($timearr[0], $timearr[1],$timearr[2]); } public static function adddate($date, $int, $unit = d) { //日期的增加 $value = array('y'=>'', 'm'=>'', 'd'=>''); $datearr = explode ( -, $date); if(array_key_exists($unit, $value)){ $value[$unit] = $int; }else{ return false; } return date (y-m-d, mktime (0, 0, 0, $datearr[1] + $value['m'], $datearr[2] + $value['d'], $datearr[0] +$value['y'])); } public static function adddatetime($date, $int, $unit = d) { //日期的增加 $value = array('y'=>'', 'm'=>'', 'd'=>'', 'h'=>'', 'i'=>''); $datearr = preg_split ( /-|\s|:/, $date); if(array_key_exists($unit, $value)){ $value[$unit] = $int; }else{ return false; } 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'])); } public static function adddaytimestamp($ntime, $aday) { //取当前时间后几天,天数增加单位为1 $dayst = 3600 * 24; $oktime = $ntime + ($aday * $dayst); return $oktime; } public static function datediff($begin, $end, $unit = d) { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天 $diff = strtotime($end) - strtotime($begin); switch($unit) { case y: $retval = bcdiv($diff, (60 * 60 * 24 * 365)); break; case m: $retval = bcdiv($diff, (60 * 60 * 24 * 30)); break; case w: $retval = bcdiv($diff, (60 * 60 * 24 * 7)); break; case d: $retval = bcdiv($diff, (60 * 60 * 24)); break; case h: $retval = bcdiv($diff, (60 * 60)); break; case i: $retval = bcdiv($diff, 60); break; case s: $retval = $diff; break; } return $retval; } public static function getweekday($date, $separator = -) { //计算出给出的日期是星期几 $datearr = explode ($separator, $date); return date (w, mktime ( 0, 0, 0, $datearr[1], $datearr[2], $datearr[0])); } public static function timefromnow($dateline) { //让日期显示为:xx天xx年以前 if(empty($dateline)) return false; $seconds = time() - $dateline; if($seconds return 1分钟前; }elseif($seconds return floor($seconds/60).分钟前; }elseif($seconds return floor($seconds/3600).小时前; }elseif($seconds return date(昨天 h:i, $dateline).; }else{ return date('y-m-d', $dateline); } } public static function transdatetochs($date) { if (empty ($date)) return '今日'; date_default_timezone_set('prc'); $dates = date ('y年m月d日', strtotime ($date)); return $dates; } // 08/31/2004 => 2004-08-31 public static function transdateui($datestr, $type = 'y-m-d') { if ($datestr == null) return null; $target = $datestr; $arr_date = preg_split ( /\//, $target); $monthstr = $arr_date[0]; $daystr = $arr_date[1]; $yearstr = $arr_date[2]; $result = date ($type, mktime (0, 0, 0, $monthstr, $daystr, $yearstr)); return $result; } // 12/20/2004 10:55 am => 2004-12-20 10:55:00 public static function transdatetimeui($datestr, $type = 'y-m-d h:i:s') { if ($datestr == null) return null; $target = $datestr; $arr_date = preg_split ( /\/|\s|:/, $target); $monthstr = $arr_date[0]; $daystr = $arr_date[1]; $yearstr = $arr_date[2]; $hourstr = $arr_date[3]; $minutesstr = $arr_date[4]; $result = date ($type, mktime ($hourstr, $minutesstr, 0, $monthstr, $daystr, $yearstr)); return $result; }}?>
复制代码
其它类似信息

推荐信息