日期:2014-05-17  浏览次数:20498 次

PHP时间工具类
<?php
class DateUtils {
	function checkDate($date) { //检查日期是否合法日期
		$dateArr = explode ( "-", $date );
		if (is_numeric ( $dateArr [0] ) && is_numeric ( $dateArr [1] ) && is_numeric ( $dateArr [2] )) {
			return checkdate ( $dateArr [1], $dateArr [2], $dateArr [0] );
		}
		return false;
	}
	
	function checkTime($time) { //检查时间是否合法时间  
		$timeArr = explode ( ":", $time );
		if (is_numeric ( $timeArr [0] ) && is_numeric ( $timeArr [1] ) && is_numeric ( $timeArr [2] )) {
			if (($timeArr [0] >= 0 && $timeArr [0] <= 23) && ($timeArr [1] >= 0 && $timeArr [1] <= 59) && ($timeArr [2] >= 0 && $timeArr [2] <= 59))
				return true;
			else
				return false;
		}
		return false;
	}
	
	function addDate($date, $int, $unit = "d") { //时间的增加(还可以改进成时分秒都可以增加,有时间再补上)
		$dateArr = explode ( "-", $date );
		$value [$unit] = $int;
		return date ( "Y-m-d", mktime ( 0, 0, 0, $dateArr [1] + $value ['m'], $dateArr [2] + $value ['d'], $dateArr [0] + $value ['y'] ) );
	
	}
	
	function addDay($ntime, $aday) { //取当前时间后几天,天数增加单位为1
		$dayst = 3600 * 24;
		$oktime = $ntime + ($aday * $dayst);
		return $oktime;
	}
	
	function dateDiff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天  
		switch ($unit) {
			case 's' :
				$dividend = 1;
				break;
			case 'i' :
				$dividend = 60;
				break;
			case 'h' :
				$dividend = 3600;
				break;
			case 'd' :
				$dividend = 86400;
				break;
			default :
				$dividend = 86400;
		}
		$time1 = strtotime ( $date1 );
		$time2 = strtotime ( $date2 );
		if ($time1 && $time2)
			return ( float ) ($time1 - $time2) / $dividend;
		return false;
	}
	
	function getWeekDay($date) { //计算出给出的日期是星期几
		$dateArr = explode ( "-", $date );
		return date ( "w", mktime ( 0, 0, 0, $dateArr [1], $dateArr [2], $dateArr [0] ) );
	}
	
	function getDateTimeMk($mktime) { //取当前时间,格式为2009-10-23 14:20:35
		return MyDate ( 'Y-m-d H:i:s', $mktime );
	}
	
	function getDateMk($mktime) { //取当前时间,格式为2009-10-23
		if ($mktime == "0")
			return "暂无";
		else
			return MyDate ( "Y-m-d", $mktime );
	}
	
	function getMkTime($dtime) { //把时间格式转化为Linux时间戳
		global $cfg_cli_time;
		if (! ereg ( "[^0-9]", $dtime )) {
			return $dtime;
		}
		$dtime = trim ( $dtime );
		$dt = Array (1970, 1, 1, 0, 0, 0 );
		$dtime = ereg_replace ( "[\r\n\t]|日|秒", " ", $dtime );
		$dtime = str_replace ( "年", "-", $dtime );
		$dtime = str_replace ( "月", "-", $dtime );
		$dtime = str_replace ( "时", ":", $dtime );
		$dtime = str_replace ( "分", ":", $dtime );
		$dtime = trim ( ereg_replace ( "[ ]{1,}", " ", $dtime ) );
		$ds = explode ( " ", $dtime );
		$ymd = explode ( "-", $ds [0] );
		if (! isset ( $ymd [1] )) {
			$ymd = explode ( ".", $ds [0] );
		}
		if (isset ( $ymd [0] )) {
			$dt [0] = $ymd [0];
		}
		if (isset ( $ymd [1] )) {
			$dt [1] = $ymd [1];
		}
		if (isset ( $ymd [2] )) {
			$dt [2] = $ymd [2];
		}
		if (strlen ( $dt [0] ) == 2) {
			$dt [0] = '20' . $dt [0];
		}
		if (isset ( $ds [1] )) {
			$hms = explode ( ":", $ds [1] );
			if (isset ( $hms [0] )) {
				$dt [3] = $hms [0];
			}
			if (isset ( $hms [1] )) {
				$dt [4] = $hms [1];
			}
			if (isset ( $hms [2] )) {
				$dt [5] = $hms [2];
			}
		}
		foreach ( $dt as $k => $v ) {
			$v = ereg_replace ( "^0{1,}", '', trim ( $v ) );
			if ($v == '') {
				$dt [$k] = 0;
			}
		}
		$mt = @gmmktime ( $dt [3], $dt [4], $dt [5], $dt [1], $dt [2], $dt [0] ) - 3600 * $cfg_cli_time;
		if (! empty ( $mt )) {
			return $mt;
		} else {
			return time ();
		}
	}
	
	function MyDate($format = 'Y-m-d H:i:s', $timest = 0) { //返回格林威治标准时间
		//global $cfg_cli_time='+8';
		global $cfg_cli_time;
		$addtime = $cfg_cli_time * 3600;
		if (empty ( $format )) {
			$format = 'Y-m-d H:i:s';
		}
		return gmdate ( $format, $timest + $addtime );
	}
	
	function floorTime($seconds) { //让日期显示为:XX天XX年以前 
		$times = '';
		$days = floor ( ($seconds / 86400) % 30 );
		$hours = floor ( ($seconds / 3600)