Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a PHP DateTime difference in days, considering midnight as a day change

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
like image 667
marcv Avatar asked Aug 07 '13 11:08

marcv


People also ask

How can calculate date difference in days in PHP?

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.

How can I get the difference between two dates and hours in PHP?

You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

How can add days in date in PHP?

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");


4 Answers

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
like image 171
MajorCaiger Avatar answered Oct 18 '22 22:10

MajorCaiger


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

like image 33
x4rf41 Avatar answered Oct 19 '22 00:10

x4rf41


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");
    }
}
like image 8
MoonLite Avatar answered Oct 18 '22 23:10

MoonLite


$date1 = new DateTime("2013-08-07 13:00:00");
$date2 = new DateTime("2013-08-08 12:00:00");
  1. Drop time in DateTime's objects:
$date1->setTime(0, 0, 0);
$date2->setTime(0, 0, 0);
  1. Get difference of adapted objects:
echo $date1->diff($date2)->days;
like image 4
sprutex Avatar answered Oct 18 '22 23:10

sprutex