What is the simplest way to get the difference in days between two PHP DateTimes
, considering midnight as a day change (just like the DATEDIFF(DAY)
SQL function does)?
For example, between today at 13:00 and tomorrow at 12:00, I should get 1 (day), even though the interval is less than 24 hours.
$date1 = new DateTime("2013-08-07 13:00:00");
$date2 = new DateTime("2013-08-08 12:00:00");
echo $date1->diff($date2)->days; // 0
The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.
You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
PHP date_add() Function $date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d");
You could ignore the time portion of the date string
$date1 = new DateTime(date('Y-m-d', strtotime("2013-08-07 13:00:00")));
$date2 = new DateTime(date('Y-m-d', strtotime("2013-08-08 12:00:00")));
echo $date1->diff($date2)->days; // 1
a simple solution to this to strip the time or set it to 00:00:00
, that should always give you the desired result:
$date1 = new DateTime("2013-08-07");
$date2 = new DateTime("2013-08-08");
echo $date1->diff($date2)->days;
or
$date1 = new DateTime("2013-08-07 00:00:00");
$date2 = new DateTime("2013-08-08 00:00:00");
echo $date1->diff($date2)->days;
the time doesnt really matter here
note that DateInterval->days is always positive. hence the use of ->invert.
/**
* return amount of days between dt1 and dt2
* (how many midnights pass going from dt1 to dt2)
* 0 = same day,
* -1 = dt2 is 1 day before dt1,
* 1 = dt2 is 1 day after dt1, etc.
*
* @param \DateTime $dt1
* @param \DateTime $dt2
* @return int|false
*/
function getNightsBetween(\DateTime $dt1, \DateTime $dt2){
if(!$dt1 || !$dt2){
return false;
}
$dt1->setTime(0,0,0);
$dt2->setTime(0,0,0);
$dti = $dt1->diff($dt2); // DateInterval
return $dti->days * ( $dti->invert ? -1 : 1); // nb: ->days always positive
}
usage examples:
$dt1 = \DateTime::createFromFormat('Y-m-d', '2014-03-03' );
$dt2 = \DateTime::createFromFormat('Y-m-d', '2014-02-20' );
getNightsBetween($dt1, $dt2); // -11
$dt1 = \DateTime::createFromFormat('Y-m-d H:i:s', '2014-01-01 23:59:59' );
$dt2 = \DateTime::createFromFormat('Y-m-d H:i:s', '2014-01-02 00:00:01' );
getNightsBetween($dt1, $dt2); // 1 (only 2 seconds later, but still the next day)
$dt1 = \DateTime::createFromFormat('Y-m-d', '2014-04-09' );
$dt2 = new \DateTime();
getNightsBetween($dt1, $dt2); // xx (how many days (midnights) passed since I wrote this)
example of some text magic:
function getRelativeDay(\DateTime $dt2){
if(!$dt2){
return false;
}
$n = getNightsBetween( new \DateTime(), $dt2);
switch($n){
case 0: return "today";
case 1: return "tomorrow";
case -1: return "yesterday";
default:
return $n . (abs($n)>1?"days":"day") . ($n<0?" ago":" from now");
}
}
$date1 = new DateTime("2013-08-07 13:00:00");
$date2 = new DateTime("2013-08-08 12:00:00");
$date1->setTime(0, 0, 0);
$date2->setTime(0, 0, 0);
echo $date1->diff($date2)->days;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With