Question

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
Was it helpful?

Solution

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

OTHER TIPS

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

this example can help you:

   $date1 = date_create($d1);
$date2 = date_create($d2);
//$FromFullDateTime=$from.$FromTime;

$date1_month = date_format($date1, 'd');
$date2_month = date_format($date2, 'd');
$dif = $date2_month - $date1_month;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top