如何计算PHP中两个date之间的天数?
如果我有几个string$ startDate和$ endDate,它们被设置为(例如)“2011/07/01”和“2011/07/17”(意思是2011年7月1日和2011年7月17日)。 我如何计算从开始date到结束date? 在给出的例子中,这将是17天。
这是做到这一点的原始方法
$startTimeStamp = strtotime("2011/07/01"); $endTimeStamp = strtotime("2011/07/17"); $timeDiff = abs($endTimeStamp - $startTimeStamp); $numberDays = $timeDiff/86400; // 86400 seconds in one day // and you might want to convert to integer $numberDays = intval($numberDays);
使用DateTime::diff
(又名date_diff
):
$datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2);
要么:
$datetime1 = date_create('2009-10-11'); $datetime2 = date_create('2009-10-13'); $interval = date_diff($datetime1, $datetime2);
然后你可以通过调用$interval->days
来获得整数的$interval->days
。
PHP有一个date_diff()函数来做到这一点。
<?php $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%R%a days'); ?>
资料来源: http : //www.php.net/manual/en/datetime.diff.php
万一您的date时间也有小时:分钟:秒,你仍然想要有天数..
/** * Returns the total number of days between to DateTimes, * if it is within the same year * @param $start * @param $end */ public function dateTimesToDays($start,$end){ return intval($end->format('z')) - intval($start->format('z')) + 1; }
没有解决scheme为我工作。 对于那些仍然在PHP 5.2(DateTime :: diff在5.3中引入)的人,这个解决scheme的工作原理是:
function howDays($from, $to) { $first_date = strtotime($from); $second_date = strtotime($to); $offset = $second_date-$first_date; return floor($offset/60/60/24); }
如果您想知道天数(如果有),小时数(如果有),小数点(如果有)和秒数,则可以执行以下操作:
$previousTimeStamp = strtotime("2011/07/01 21:12:34"); $lastTimeStamp = strtotime("2013/09/17 12:34:11"); $menos=$lastTimeStamp-$previousTimeStamp; $mins=$menos/60; if($mins<1){ $showing= $menos . " seconds ago"; } else{ $minsfinal=floor($mins); $secondsfinal=$menos-($minsfinal*60); $hours=$minsfinal/60; if($hours<1){ $showing= $minsfinal . " minutes and " . $secondsfinal. " seconds ago"; } else{ $hoursfinal=floor($hours); $minssuperfinal=$minsfinal-($hoursfinal*60); $days=$hoursfinal/24; if($days<1){ $showing= $hoursfinal . "hours, " . $minssuperfinal . " minutes and " . $secondsfinal. " seconds ago"; } else{ $daysfinal=floor($days); $hourssuperfinal=$hoursfinal-($daysfinal*24); $showing= $daysfinal. "days, " .$hourssuperfinal . " hours, " . $minssuperfinal . " minutes and " . $secondsfinal. " seconds ago"; }}} echo $showing;
如果你想增加几个月和几年,你可以使用相同的逻辑。
简单的方法是,
$currentdate = date('Ymd H:i:s'); $after1yrdate = date("Ymd H:i:s", strtotime("+1 year", strtotime($data))); $diff = (strtotime($after1yrdate) - strtotime($currentdate)) / (60 * 60 * 24); echo "<p style='color:red'>The difference is ".round($diff)." Days</p>";
$date1 = date_create("2017-04-15"); $date2 = date_create("2017-05-18"); //difference between two dates $diff = date_diff($date1,$date2); //count days echo 'Days Count - '.$diff->format("%a");
您可以使用date_diff
来计算两个date之间的差异:
$date1 = date_create("2013-03-15"); $date2 = date_create("2013-12-12"); $diff = date_diff($date1 , $date2); echo $diff->format("%R%a days");