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.
-
see http://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates – Eric Lavoie Jan 04 '16 at 20:16
11 Answers
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);

- 7,370
- 9
- 54
- 71
-
4Will not work correctly when you have local dates that include a dayling savings time switchover, because on those dates you have days with 23 or 25 hours. – Michael Borgwardt Sep 06 '10 at 19:53
-
Actually, it says 16 days for those dates.. Do I need to make dates ignore daylight saving time..? – cannyboy Sep 06 '10 at 22:43
-
It should be 16 days because July 1 and July 17 are 16 days apart whereas July 1 and July 18 are 17 days apart – axsuul Sep 06 '10 at 23:09
-
@MichaelBorgwardt you have two solid dates, I don't see how daylight saving can affect this. – pronebird Mar 19 '14 at 22:30
-
2@Andy: if by "solid" you mean "literal", sure, but the question says "for instance", not "I need a solution for these specific dates in 2011 and don't care for any problems that might arise with other dates". – Michael Borgwardt Mar 19 '14 at 22:42
-
-
They say it's better to use the `Datetime` class, since `strtotime` can't compare dates further than 2038 year ... – Pathros Jun 02 '17 at 18:36
Use DateTime::diff
(aka 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
.

- 11,285
- 1
- 43
- 60
-
That doesn't seem to work with my initial strings of "2011/07/01" and "2011/07/17" ... "Call to undefined function date_diff().." . I'm using PHP 5.2.10 – cannyboy Sep 06 '10 at 21:40
-
4Per the documentation (linked at the top), this is PHP >= 5.3.0. For next time, please include the PHP version in your question. – wuputah Sep 06 '10 at 23:13
-
does only work if you don't have a time in your Date Times otherwise it may return 1-2 days to few – Dukeatcoding Jan 22 '14 at 14:09
-
2bug : $interval->days return 6015 days ! And if i use the $interval->format("%d"), it only return the differences between the days, without taking into account the months and years. – Alex Sep 02 '14 at 14:54
-
1Note that you need to add 1 to the result if you want to include both the start and end day in the count. – Andrew Downes Nov 18 '17 at 08:53
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

- 51,422
- 11
- 85
- 111
In case your DateTime has also hour:minutes:seconds and you still want to have the number of days..
/**
* 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;
}

- 1,363
- 2
- 20
- 34
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:
function howDays($from, $to) {
$first_date = strtotime($from);
$second_date = strtotime($to);
$offset = $second_date-$first_date;
return floor($offset/60/60/24);
}

- 544
- 4
- 10

- 455
- 2
- 4
- 9
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.

- 11
- 3
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>";

- 14,400
- 15
- 47
- 66

- 176
- 1
- 7
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:19 and End Date 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
)
)

- 2,232
- 22
- 21
You can use date_diff
to calculate the difference between two dates:
$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1 , $date2);
echo $diff->format("%R%a days");

- 913
- 1
- 12
- 24