php 如何计算PHP中两个日期之间的天数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3653882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count days between two dates in PHP?
提问by cannyboy
If I have a couple of strings $startDate
and $endDate
which are set to (for instance) "2011/07/01"
and "2011/07/17"
(meaning 1 July 2011 and 17 July 2011). How would I count the days from start date to end date? In the example given, it would be 17 days.
如果我有几个字符串$startDate
并且$endDate
设置为(例如)"2011/07/01"
和"2011/07/17"
(意味着 2011 年 7 月 1 日和 2011 年 7 月 17 日)。我如何计算从开始日期到结束日期的天数?在给出的示例中,它将是 17 天。
回答by axsuul
Here is the raw way to do it
这是执行此操作的原始方法
$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);
回答by wuputah
Use DateTime::diff
(aka date_diff
):
使用DateTime::diff
(又名date_diff
):
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
Or:
或者:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
You can then get the interval as a integer by calling $interval->days
.
然后,您可以通过调用以整数形式获取间隔$interval->days
。
回答by Paul Schreiber
PHP has a date_diff() function to do this.
PHP 有一个 date_diff() 函数可以做到这一点。
回答by Layke
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>
回答by Dukeatcoding
In case your DateTime has also hour:minutes:seconds and you still want to have the number of days..
如果您的 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;
}
回答by khtheitroad
$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");
回答by Tomás Crespo García
None of the solutions worked for me. For those who are still on PHP 5.2 (DateTime::diff was introduced in 5.3), this solution works:
没有一个解决方案对我有用。对于那些仍然使用 PHP 5.2(DateTime::diff 在 5.3 中引入)的人,这个解决方案有效:
function howDays($from, $to) {
$first_date = strtotime($from);
$second_date = strtotime($to);
$offset = $second_date-$first_date;
return floor($offset/60/60/24);
}
回答by Jose María Simón
If you want to know the number of days (if any), the number of hours (if any), minutues (if any) and seconds, you can do the following:
如果您想知道天数(如果有的话)、小时数(如果有的话)、分钟数(如果有的话)和秒数,您可以执行以下操作:
$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;
You could use the same logic if you want to add months and years.
如果您想添加月份和年份,您可以使用相同的逻辑。
回答by Archa
Simple way to count is,
简单的计算方法是,
$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>";
回答by Renish Gotecha
i created a function in which if you pass two dates than it will return day wise value. For better understanding please see the output of start date : 2018-11-12 11:41:19and End Date 2018-11-16 12:07:26
我创建了一个函数,在该函数中,如果您传递两个日期,它将返回按天计算的值。为了更好地理解,请参阅开始日期的输出: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;
}
Output
输出
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
)
)