如果我有一些字符串$startDate
和$endDate
设置为(例如)"2011/07/01"
和"2011/07/17"
(意味着2011年7月1日和2011年7月17日) 。我如何计算从开始日期到结束日期的天数?在给出的例子中,它将是17天。
答案 0 :(得分:40)
这是实现这一目标的原始方式
$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);
答案 1 :(得分:32)
Use 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
来获取整数的间隔。
答案 2 :(得分:3)
PHP有一个date_diff()函数来执行此操作。
答案 3 :(得分:3)
如果您的DateTime还有小时:分钟:秒,您仍然希望有天数..
/**
* 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;
}
答案 4 :(得分:2)
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
答案 5 :(得分:1)
这些解决方案都不适合我。对于仍然使用PHP 5.2的人(5.3中引入了DateTime :: diff),此解决方案可以工作:
function howDays($from, $to) {
$first_date = strtotime($from);
$second_date = strtotime($to);
$offset = $second_date-$first_date;
return floor($offset/60/60/24);
}
答案 6 :(得分:0)
如果您想知道天数(如果有),小时数(如果有的话),minutues(如果有的话)和秒数,您可以执行以下操作:
$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;
如果要添加月份和年份,可以使用相同的逻辑。
答案 7 :(得分:0)
简单的计算方法是,
$currentdate = date('Y-m-d H:i:s');
$after1yrdate = date("Y-m-d 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>";
答案 8 :(得分:0)
$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");
答案 9 :(得分:0)
我创建了一个函数,如果您传递两个日期,它将返回按天计算的值。为了更好地理解,请查看开始日期的输出: 2018-11-12 11:41:19 和结束日期 2018-11-16 12:07:26 >
private function getTimeData($str1,$str2){
$datetime1 = strtotime($str1);
$datetime2 = strtotime($str2);
$myArray = array();
if(date('d', $datetime2) != date('d', $datetime1) || date('m', $datetime2) != date('m', $datetime1) || date('y', $datetime2) != date('y', $datetime1)){
$exStr1 = explode(' ',$str1);
$exStr2 = explode(' ',$str2);
$datediff = strtotime($exStr2[0]) - strtotime($exStr1[0]);
$totalDays = round($datediff / (60 * 60 * 24));
$actualDate1 = $datetime1;
$actualDate2 = date('Y-m-d', $datetime1)." 23:59:59";
$interval = abs(strtotime($actualDate2)-$actualDate1);
$minutes = round($interval / 60);
$myArray[0]['startDate'] = date('Y-m-d H:i:s', $actualDate1);
$myArray[0]['endDate'] = $actualDate2;
$myArray[0]['minutes'] = $minutes;
$i = 1;
if($totalDays > 1){
for($i=1; $i<$totalDays; $i++){
$dayString = "+".$i." day";
$edate = strtotime($dayString, $actualDate1);
$myArray[$i]['startDate'] = date('Y-m-d', $edate)." 00:00:00";
$myArray[$i]['endDate'] = date('Y-m-d', $edate)." 23:59:59";
$myArray[$i]['minutes'] = 1440;
}
}
$actualSecDate1 = date('Y-m-d', $datetime2)." 00:00:00";
$actualSecDate2 = $datetime2;
$interval = abs(strtotime($actualSecDate1)-$actualSecDate2);
$minutes = round($interval / 60);
$myArray[$i]['startDate'] = $actualSecDate1;
$myArray[$i]['endDate'] = date('Y-m-d H:i:s', $actualSecDate2);
$myArray[$i]['minutes'] = $minutes;
}
else{
$interval = abs($datetime2-$datetime1);
$minutes = round($interval / 60);
$myArray[0]['startDate'] = date('Y-m-d H:i:s', $datetime1);
$myArray[0]['endDate'] = date('Y-m-d H:i:s', $datetime2);
$myArray[0]['minutes'] = $minutes;
}
return $myArray;
}
输出
Array
(
[0] => Array
(
[startDate] => 2018-11-12 11:41:19
[endDate] => 2018-11-12 23:59:59
[minutes] => 739
)
[1] => Array
(
[startDate] => 2018-11-13 00:00:00
[endDate] => 2018-11-13 23:59:59
[minutes] => 1440
)
[2] => Array
(
[startDate] => 2018-11-14 00:00:00
[endDate] => 2018-11-14 23:59:59
[minutes] => 1440
)
[3] => Array
(
[startDate] => 2018-11-15 00:00:00
[endDate] => 2018-11-15 23:59:59
[minutes] => 1440
)
[4] => Array
(
[startDate] => 2018-11-16 00:00:00
[endDate] => 2018-11-16 12:07:26
[minutes] => 727
)
)
答案 10 :(得分:-1)
您可以使用date_diff
来计算两个日期之间的差异:
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1 , $date2);
echo $diff->format("%R%a days");