PHP获取开始日期和结束日期之间的天数
如果使用PHP 5.3,则可以使用新的DateTime
类:
$startDate = new DateTime("20101013");
$endDate = new DateTime("20101225");
$interval = $startDate->diff($endDate);
echo $interval->days . " until Christmas"; // echos 73 days until Christmas
如果不是,您将需要使用strtotime
:
$startDate = strtotime("20101013");
$endDate = strtotime("20101225");
$interval = $endDate - $startDate;
$days = floor($interval/(60 * 60 * 24));
echo $days . " until Christmas"; // echos 73 days until Christmas
新的DateTime,DateTimeInterval,DateTimePeriod和DateTimeZone类只是摇滚。我也与他们取得了一些经验。我认为它比时间戳更好,因为它考虑了闰年,跨越式等等。所以+1为你。我也在这篇文章中给出了一个更详细的例子:http://*.com/questions/3108591/calculate-number-of-hours-between-2-dates-in-php/3108800#3108800 – 2010-10-13 08:40:06
谢谢你,寂寞天。 – Francisc 2010-10-13 09:56:51
$DayDiff = strtotime("2010-01-12")-strtotime("2009-12-30");
echo date('z', $DayDiff)." Days";
这一个应该是精确和可使用用PHP < 5.2
下面是示例代码
$startDate = mktime(0,0,0,1,1,2010);
$endDate = mktime(0,0,0,12,1,2010);
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days";
<?php
$time1=strtotime($startDate);
$time2=strtotime($endDate);
$daycount=floor(($time2-$time1)/ 86400);
?>
<?php
function days($date1, $date2) {
$date1 = strtotime($date1);
$date2 = strtotime($date2);
return ($date2 - $date1)/(24 * 60 * 60);
}
$date1 = '20100820';
$date2 = '20100930';
echo days($date1, $date2);
?>
我发现让他们之间的天数最简单的方法是通过转换开始和结束日期的Unix时间戳和对他们做一个减法。
然后,如果要格式化日期,请使用PHP日期函数将其转换回来。
这是我的方法,基于在大多数情况下的残酷搜索,仅仅因为以秒为单位(几周,几个月,几年)的分割可能不会返回精确的结果,例如以闰年为例。
<?php
function datediff($timeformat, $startdate, $enddate)
{
$unix_startdate = strtotime($startdate) ;
$unix_enddate = strtotime($enddate) ;
$min_date = min($unix_startdate, $unix_enddate);
$max_date = max($unix_startdate, $unix_enddate);
$Sd = date("d", $unix_startdate) ;
$Sm = date("m", $unix_startdate) ;
$Sy = date("Y", $unix_startdate) ;
$Ed = date("d", $unix_enddate) ;
$Em = date("m", $unix_enddate) ;
$Ey = date("Y", $unix_enddate) ;
$unixtimediff = $unix_enddate - $unix_startdate ;
if ($unixtimediff <= 0) return -1 ;
switch(strtolower($timeformat))
{
case "d": // days
$divisor = 3600 * 24 ;
return floor($unixtimediff/$divisor) + 1 ;
break ;
case "w": // weeks
$i = 0 ;
while (($min_date = strtotime("+1 DAY", $min_date)) <= $max_date) $i++;
return floor($i/7) ;
break ;
case "m": // months
$i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) $i++;
return $i ;
break ;
case "q": // quaterly (3 months)
$i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
while (($min_date = strtotime("+3 MONTH", $min_date)) <= $max_date) $i++;
return $i ;
break ;
case "y": // year
$i = $Sd != $Ed && $Sm != $Em ? 1 : 0 ;
while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) $i++;
return floor($i/12) ;
break ;
}
}
$startdate = "2014-01-01" ;
$enddate = "2015-12-31" ;
$formats = array("d" => "days", "w" => "weeks", "m" => "months", "q" => "quaterly", "y" => "years") ;
foreach($formats AS $K => $F)
echo "From $startdate to $enddate in $F format: ". datediff("$K", $startdate, $enddate)."<br>" ;
?>
请注明,如果你想两天包括在内或排除两个日期的数量。 – stillstanding 2010-10-13 08:35:58
包括开始和结束,谢谢。 – Francisc 2010-10-13 08:44:40