用PHP获取两个日期之间间隔的天数


方法一:

<?php
	$datetime1 = date_create('2013-1-28');
	$datetime2 = date_create('2014-05-26');
	$interval = date_diff($datetime1, $datetime2);
	echo $interval->format('%R%a days');
?>

方法二:

<?php
	$datetime1 = new DateTime('2013-10-21');
	$datetime2 = new DateTime('2014-1-5');
	$interval = $datetime1->diff($datetime2);
	echo $interval->format('%R%a days');
?>

方法三:

<?php
function daysbetweendates($date1, $date2){
	$date1 = strtotime($date1);
	$date2 = strtotime($date2);
	$days = ceil(abs($date1 - $date2)/86400);
	return $days;
}
$date1 = "2014-05-26";
$date2 = "2013-10-21";
echo daysbetweendates($date1, $date2) . " days.";

function DateDiff($interval, $date1,$date2) {
	// 得到两日期之间间隔的秒数
	//echo "#".(strtotime("20040623")-strtotime("20040617"))/24/3600;
	//echo "<BR>";
	$timedifference = abs(strtotime($date2) -strtotime($date1));
	//echo "@".date( "Y-m-d H:i:s",strtotime($date2))."@".date( "Y-m-d H:i:s",strtotime($date1));
	//echo "@".$timedifference;
	switch ($interval) {
	case "w": $retval = round($timedifference/604800); break;//得到两日期之间间隔的星期数
	case "d": $retval = round($timedifference/86400); break;//得到两日期之间间隔的日数
	case "h": $retval = round($timedifference/3600); break;//得到两日期之间间隔的小时数
	case "n": $retval = round($timedifference/60); break;//得到两日期之间间隔的分钟数
	case "s": $retval = $timedifference; break; //得到两日期之间间隔的秒数
	}
	return $retval;
}
echo DateDiff("d", $date1, $date2)." days";
?>

起先就是好奇,然后在网上搜索了之后,自己尝试了OK就行,满足了好奇心吧(☆_☆)/~~

,

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注