0
点赞
收藏
分享

微信扫一扫

PHP 时间函数:与当前时间比…

//比较两个时间的函数,值为正/负/0
function get2DT1SubDT2Operate($DateTime1, $DateTime2= " ", $RetFormat= "S ")
{
//如果$DateTime2为空,设置为当前的时间
if ($DateTime2 == " ") $DateTime2 = date( "Y-m-d //格式化时间
$DateTimeTemp = strftime( "%Y-%m-%d",strtotime($DateTime1));//格式化字符串
//$DateTimeTemp = strftime( "%Y-%m-%d %H:%M:%S ",strtotime($DateTime1));//格式化字符串
$iTemp[ "Y "] = intval(substr($DateTimeTemp,0,4));
$iTemp[ "M "] = intval(substr($DateTimeTemp,5,2));
$iTemp[ "D "] = intval(substr($DateTimeTemp,8,2));
$iTemp[ "H "] = intval(substr($DateTimeTemp,11,2));
$iTemp[ "I "] = intval(substr($DateTimeTemp,14,2));
$iTemp[ "S "] = intval(substr($DateTimeTemp,17,2));
$aDT[] = $iTemp;
unset($iTemp);
$DateTimeTemp = strftime( "%Y-%m-%d ",strtotime($DateTime2));//格式化字符串

//$DateTimeTemp = strftime( "%Y-%m-%d %H:%M:%S ",strtotime($DateTime2));//格式化字符串
$iTemp[ "Y "] = intval(substr($DateTimeTemp,0,4));
$iTemp[ "M "] = intval(substr($DateTimeTemp,5,2));
$iTemp[ "D "] = intval(substr($DateTimeTemp,8,2));
$iTemp[ "H "] = intval(substr($DateTimeTemp,11,2));
$iTemp[ "I "] = intval(substr($DateTimeTemp,14,2));
$iTemp[ "S "] = intval(substr($DateTimeTemp,17,2));
$aDT[] = $iTemp;
//$DateTime2 - $DateTime1的时间差值,单位秒
$iSec = mktime($aDT[1][ "H "], $aDT[1][ "I "], $aDT[1][ "S "],
$aDT[1][ "M "], $aDT[1][ "D "], $aDT[1][ "Y "]) -
mktime($aDT[0][ "H "], $aDT[0][ "I "], $aDT[0][ "S "],
$aDT[0][ "M "], $aDT[0][ "D "], //格式化输出值
if ($RetFormat == "S ")
return $iSec;
elseif ($RetFormat == "I ")
return $iSec / 60;
elseif ($RetFormat == "H ")
return $iSec / (60 * 60);
elseif ($RetFormat == "D ")
return $iSec / (60 * 60 * 24);
}//输出值
echo get2DT1SubDT2Operate( "2012-10-22", "".date('c')."", "D ")
?>

//检查时间A是否符合format的函数
function chkDateFormat($format, $strTime) {
$tem = date ( $format, strtotime ( $strTime ) );
if ($strTime == $tem)
return true;
else
return false;
}if (! chkDateFormat ( 'Y-m-d H:i', '2009/08/19 12:00' ))

echo "时间格式有误!";

//将某时间与当前时间比较的函数
function isBeforeToday($strTime) {

if (strtotime ( $strTime ) < time ())
return true;
else
return false;
}if (isBeforeToday ( '2008/08/19 12:00' ))

echo "该时间已过期";

//比较两个时间的函数
function aLessThanB($strTime1, $strTime2) {

if (strtotime ( $strTime1 ) < strtotime ( $strTime2 ))
return true;
else
return false;
}if(aLessThanB('2008/08/19','2009/08/19'))
echo "A 时间早于B时间";

举报

相关推荐

0 条评论