DateTime::diff

DateTimeImmutable::diff

DateTimeInterface::diff

date_diff

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateTime::diff -- DateTimeImmutable::diff -- DateTimeInterface::diff -- date_diffReturns the difference between two DateTime objects

说明

面向对象风格

public DateTime::diff ( DateTimeInterface $targetObject , bool $absolute = false ) : DateInterval
public DateTimeImmutable::diff ( DateTimeInterface $targetObject , bool $absolute = false ) : DateInterval
public DateTimeInterface::diff ( DateTimeInterface $targetObject , bool $absolute = false ) : DateInterval

过程化风格

date_diff ( DateTimeInterface $baseObject , DateTimeInterface $targetObject , bool $absolute = false ) : DateInterval

Returns the difference between two DateTimeInterface objects.

参数

datetime

The date to compare to.

absolute

Should the interval be forced to be positive?

返回值

The DateInterval object represents the difference between the two dates 或者在失败时返回 false.

The return value more specifically represents the interval to apply to the original object ($this or $originObject) to arrive at the $targetObject. This process is not always reversible.

范例

Example #1 DateTime::diff() example

面向对象风格

<?php
$origin 
= new DateTime('2009-10-11');
$target = new DateTime('2009-10-13');
$interval $origin->diff($target);
echo 
$interval->format('%R%a days');
?>

过程化风格

<?php
$origin 
date_create('2009-10-11');
$target date_create('2009-10-13');
$interval date_diff($origin$target);
echo 
$interval->format('%R%a days');
?>

以上例程会输出:

+2 days

Example #2 DateTime object comparison

Note:

DateTime objects can be compared using comparison operators.

<?php
$date1 
= new DateTime("now");
$date2 = new DateTime("tomorrow");

var_dump($date1 == $date2);
var_dump($date1 $date2);
var_dump($date1 $date2);
?>

以上例程会输出:

bool(false)
bool(true)
bool(false)

参见

  • DateInterval::format() - Formats the interval
  • DateTime::add() - 给一个 DateTime 对象增加一定量的天,月,年,小时,分钟 以及秒。
  • DateTime::sub() - 对一个 DateTime 对象减去一定量的 日、月、年、小时、分钟和秒。

User Contributed Notes

anonymous 10-Sep-2020 12:03
<?php
$date1
= new DateTime('2015-06-22T10:40:25', new DateTimeZone('Europe/London'));
$date2 = new DateTime("2010-01-01 22:10:57",new DateTimeZone('Asia/Kathmandu'));

$interval = date_diff($date1, $date2);

print
$interval->format("Days = %a Time = %H:%I:%S .");
?>
Rainer Perske 04-Jul-2020 11:02
PHP uses Unix timestamps internally.
Leap seconds are ignored.
monirul 15-Jul-2019 08:31
$strStart = '2013-06-19 18:25';
$strEnd   = '06/21/13 21:47';

$dteStart = new DateTime($strStart);
$dteEnd   = new DateTime($strEnd);
 $dteDiff  = $dteStart->diff($dteEnd);
 print $dteDiff->format("%H:%I:%S");

this script not comparing the date, its working only for the time
wildpenguin at gmail dot com 06-Apr-2018 09:02
Be careful when using the difference between 'Now' and a future value.
Example:
// imagine it is 2018-04-20
$date1 = new DateTime('now');
$date2 = new DateTime(date('Y-m-d'));
$date3 = new DateTime("2018-04-30"); // future

echo $date1->diff($date3)->days; // 9 days
echo $date2->diff($date3)->days; // 10 days
sloanlance+php dot net at gmail dot com 14-Jan-2018 06:05
When getting the difference between two DateTime objects with fractions of seconds, DateTime::diff() works under PHP 7.1. However, under PHP 5.6, the fraction is truncated. It's not clear whether the truncation happens when getting the DateTime objects' values, during the calculation, or immediately before returning the result.
Daniel Klein 10-May-2017 03:29
WARNING!!!

Although you CAN directly compare DateTime objects, you will get nonintuitive results if the other object is not also DateTime compatible.

I just found a subtle bug in my code because it was comparing a date against an uninitialised variable.

<?php
$now
= new DateTime();
$other_date = null;
var_dump($now < $other_date);   // false
var_dump($now > $other_date);   // true
?>

It's better to use DateTime::diff() because the return value will only be a DateInterval object if the types were compatible, otherwise it will be false.

<?php
$then
= new DateTime('yesterday');
$now = new DateTime();
$other_date = null;
var_dump($now->diff($other_date));  // false
var_dump($now->diff($then));    // DateInterval object
?>

If a DateInterval object was returned, you can check the 'invert' property to see if the second date is before the first date or not. DateInterval::invert will be 1 if the second date is before the first date, and 0 if the the second date is on or after the first date.
Anonymous 17-Mar-2017 03:34
<?php
$datetime1
= new DateTime('2016-11-30');
$datetime2 = new DateTime('2017-03-01');
$interval = $datetime1->diff($datetime2);
var_dump($interval->days / 30.4375);
$month = $interval->m + $interval->y * 12;
var_dump($month);

Result:
float(2.9897330595483)
int(2) <-- Where is the third month? February has 28/29 days total.

For
<?
php
$datetime1
= new DateTime('2016-11-30');
$datetime2 = new DateTime('2017-03-02');

Result:
float(3.0225872689938)
int(3)
csongor at halmai dot hu 02-Nov-2016 02:09
Be careful, the behaviour depends on the time zones in a weird way.

<?php
   
function printDiff($tz) {
       
$d1 = new DateTime("2015-06-01", new DateTimeZone($tz));
       
$d2 = new DateTime("2015-07-01", new DateTimeZone($tz));
       
$diff = $d1->diff($d2);
        print(
$diff->format("Year: %Y Month: %M Day: %D"). PHP_EOL);
    }
   
printDiff("UTC");
   
printDiff("Australia/Melbourne");
?>

The result is different:

Year: 00 Month: 01 Day: 00
Year: 00 Month: 00 Day: 30
arunajamal at yahoo dot com 15-Jan-2016 04:32
Similar to what was mentioned by  ianlenmac at gmail dot com
I think its also worth mentioning to note that date_diff($datetime1, $datetime2) is equivalent to " subtract $datetime1 from $datetime2 " as opposed to thinking otherwise because of the arrangement of the arguments
so date_diff($now, $tomorrow) is +ve
cagatay at devmonks dot net 25-Nov-2015 03:20
Be careful using:

$date1 = new DateTime('now');
$date2 = new DateTime('tomorrow');

$interval = date_diff($date1, $date2);

echo $interval->format('In %a days');

In some situations, this won't say "in 1 days", but "in 0 days".
I think this is because "now" is the current time, while "tomorrow" is the current day +1 but at a default time, lets say:

Now: 08:00pm, 01.01.2015
Tomorrow: 00:00am, 02.01.2015

In this case, the difference is not 24 hour, so it will says 0 days.

Better use "today", which should also use a default value like:

Today: 00:00am, 01.01.2015
Tomorrow: 00:00am, 02.01.2015

which now is 24 hour and represents 1 day.

This may sound logical and many will say "of course, this is right", but if you use it in a naiv way (like I did without thinking), you can come to this moment and facepalm yourself.

Conclusion: "Now" is "Today", but in a different clock time, but still the same day!
devegpat at gmail dot com 17-Jul-2015 03:53
<?PHP
/*small script to check if the given date is between the limit
$gap is the limit needed
date format taken is d-m-yyyy
using PDO
*/

date_default_timezone_set('Asia/kolkata');

$gap = 60;
$givendate = '7-9-2015';
$givendate = explode('-', $givendate);

$thisday    = $givendate[0];
$thismonth  = $givendate[1];
$thisyear   = $givendate[2];

$date1 = new DateTime(date('d-m-Y',mktime(0,0,0,$thismonth,$thisday,$thisyear)));
$date2 = new DateTime(date('d-m-Y'));
$differ = $date1->diff($date2);
$daysbetween = $differ->format('%a');

echo
$daysbetween;

if(
$daysbetween > $gap){
 
printf("Enter a date within next %d days the difference is %d",$gap,$daysbetween);
}
else{
 
printf("Date is valid and difference  is %d",$daysbetween);
}
?>
qrworld.net 02-Dec-2014 02:31
Here you have in this post http://softontherocks.blogspot.com/2014/12/calcular-la-edad-con-php.html the code to get the age of a person specifying the date of birth:

function getAge($birthdate){
    $adjust = (date("md") >= date("md", strtotime($birthdate))) ? 0 : -1; // Si aún no hemos llegado al día y mes en este a?o restamos 1
    $years = date("Y") - date("Y", strtotime($birthdate)); // Calculamos el número de a?os
    return $years + $adjust; // Sumamos la diferencia de a?os más el ajuste
}
starsniper at in dot com 08-Jun-2014 12:49
Another Method to compare dates:
<?php       
    $d1
= new DateTime('2014-06-07 20:56:00');
   
$d2 = new DateTime('2014-06-09 21:09:00');
    echo
'The DateTimes are: <br /> d1: '.$d1->format("d-M-y, h:i A").'<br /> d2: '.$d2->format("d-M-y, h:i A");
   
   
$date_diff = $d1->diff($d2);            // difference of dates =  ($d2 - $d1)
   
   
if ( (int)$date_diff->format('%R%a')  >= 0 ){
        echo
'<br /><br />The recent date is: '.$d2->format("d-M-y, h:i A");
        echo
'<br /> The older date is: '.$d1->format("d-M-y, h:i A");
    }else{
        echo
'<br /><br />The recent date is: '.$d1->format("d-M-y, h:i A");
        echo
'<br /> The older date is: '.$d2->format("d-M-y, h:i A");
    }
?>

Output:
The DateTimes are:
d1: 07-Jun-14, 08:56 PM
d2: 09-Jun-14, 09:09 PM

The recent date is: 09-Jun-14, 09:09 PM
The older date is: 07-Jun-14, 08:56 PM
nospam at oece dot me 20-Apr-2014 05:54
Using the identical (===) comparision operator in different but equal objects will return false

<?php
$c
= new DateTime( '2014-04-20' );
$d = new DateTime( '2014-04-20' );
var_dump( $d === $d ); #true
var_dump( $d === $c ); #false
var_dump( $d == $c ); #true
?>
crayonviolent at phpfreaks dot com 18-Mar-2014 05:26
It seems that while DateTime in general does preserve microseconds, DateTime::diff doesn't appear to account for it when comparing. 

Example:

<?php
$val1
= '2014-03-18 10:34:09.939';
$val2 = '2014-03-18 10:34:09.940';

$datetime1 = new DateTime($val1);
$datetime2 = new DateTime($val2);
echo
"<pre>";
var_dump($datetime1->diff($datetime2));

if(
$datetime1 > $datetime2)
  echo
"1 is bigger";
else
  echo
"2 is bigger";
?>

The var_dump shows that there is no "u" element, and "2 is bigger" is echoed.

To work around this apparent limitation/oversight, you have to additionally compare using DateTime::format. 

Example:

<?php
if($datetime1 > $datetime2)
  echo
"1 is bigger";
else if (
$datetime1->format('u') > $datetime2->format('u'))
  echo
"1 is bigger";
else
  echo
"2 is bigger";
?>
ianlenmac at gmail dot com 20-Sep-2013 01:10
It is worth noting, IMO, and it is implied in the docs but not explicitly stated, that the object on which diff is called is subtracted from the object that is passed to diff.

i.e. $now->diff($tomorrow) is positive.
php at jcouture dot net 20-Jun-2013 01:45
After wrestling with DateTime::diff for a while it finally dawned on me the problem was both in the formatting of the input string and the formatting of the output.

The task was to calculate the duration between two date/times.

### Calculating Duration

1. Make sure you have a valid date variable.  Both of these strings are valid:

<?php

// Example

  
$strStart = '2013-06-19 18:25';
  
$strEnd   = '06/19/13 21:47';

?>

2. Next convert the string to a date variable
~~~
<?php

   $dteStart
= new DateTime($strStart);
  
$dteEnd   = new DateTime($strEnd);

?>
~~~

3. Calculate the difference
~~~
<?php

   $dteDiff 
= $dteStart->diff($dteEnd);

?>
~~~

4. Format the output
~~~
<?php

  
print $dteDiff->format("%H:%I:%S");

/*
    Outputs
   
    03:22:00
*/

?>
~~~

[Modified by moderator for clarify]
gusarov at ukr.net 07-Jun-2013 01:16
for php<5.3
<?php
$date1
= strtotime('2013-07-03 18:00:00');
$date2 = time();
$subTime = $date1 - $date2;
$y = ($subTime/(60*60*24*365));
$d = ($subTime/(60*60*24))%365;
$h = ($subTime/(60*60))%24;
$m = ($subTime/60)%60;

echo
"Difference between ".date('Y-m-d H:i:s',$date1)." and ".date('Y-m-d H:i:s',$date2)." is:\n";
echo
$y." years\n";
echo
$d." days\n";
echo
$h." hours\n";
echo
$m." minutes\n";
?>
amons dot 360 at gmail dot com 23-Mar-2013 11:00
So this function is not available for my server's PHP. I created an alternative.

Convert the datetime into time-stamps, then subtract normally, then convert the seconds to whatever you want.

<?

$date1 = new DateTime('now');
$date1->modify("-3 hours");

$date2 = new DateTime('now');

$number1 = (int)$date1->format('U');
$number2 = (int)$date2->format('U');

echo ($number2 - $number1)/60/60; // will print 3

?>

-Suleiman ALAQEL
radu dot potop at wooptoo dot com 27-Jun-2012 08:50
Keep in mind that diff will convert the two DateTime objects from local time to UTC.
sgmurphy19 08-Feb-2012 11:00
Though I found a number of people who ran into the issue of 5.2 and lower not supporting this function, I was unable to find any solid examples to get around it. Therefore I hope this can help some others:

<?php
function get_timespan_string($older, $newer) {
 
$Y1 = $older->format('Y');
 
$Y2 = $newer->format('Y');
 
$Y = $Y2 - $Y1;

 
$m1 = $older->format('m');
 
$m2 = $newer->format('m');
 
$m = $m2 - $m1;

 
$d1 = $older->format('d');
 
$d2 = $newer->format('d');
 
$d = $d2 - $d1;

 
$H1 = $older->format('H');
 
$H2 = $newer->format('H');
 
$H = $H2 - $H1;

 
$i1 = $older->format('i');
 
$i2 = $newer->format('i');
 
$i = $i2 - $i1;

 
$s1 = $older->format('s');
 
$s2 = $newer->format('s');
 
$s = $s2 - $s1;

  if(
$s < 0) {
   
$i = $i -1;
   
$s = $s + 60;
  }
  if(
$i < 0) {
   
$H = $H - 1;
   
$i = $i + 60;
  }
  if(
$H < 0) {
   
$d = $d - 1;
   
$H = $H + 24;
  }
  if(
$d < 0) {
   
$m = $m - 1;
   
$d = $d + get_days_for_previous_month($m2, $Y2);
  }
  if(
$m < 0) {
   
$Y = $Y - 1;
   
$m = $m + 12;
  }
 
$timespan_string = create_timespan_string($Y, $m, $d, $H, $i, $s);
  return
$timespan_string;
}

function
get_days_for_previous_month($current_month, $current_year) {
 
$previous_month = $current_month - 1;
  if(
$current_month == 1) {
   
$current_year = $current_year - 1; //going from January to previous December
   
$previous_month = 12;
  }
  if(
$previous_month == 11 || $previous_month == 9 || $previous_month == 6 || $previous_month == 4) {
    return
30;
  }
  else if(
$previous_month == 2) {
    if((
$current_year % 4) == 0) { //remainder 0 for leap years
     
return 29;
    }
    else {
      return
28;
    }
  }
  else {
    return
31;
  }
}

function
create_timespan_string($Y, $m, $d, $H, $i, $s)
{
 
$timespan_string = '';
 
$found_first_diff = false;
  if(
$Y >= 1) {
   
$found_first_diff = true;
   
$timespan_string .= pluralize($Y, 'year').' ';
  }
  if(
$m >= 1 || $found_first_diff) {
   
$found_first_diff = true;
   
$timespan_string .= pluralize($m, 'month').' ';
  }
  if(
$d >= 1 || $found_first_diff) {
   
$found_first_diff = true;
   
$timespan_string .= pluralize($d, 'day').' ';
  }
  if(
$H >= 1 || $found_first_diff) {
   
$found_first_diff = true;
   
$timespan_string .= pluralize($H, 'hour').' ';
  }
  if(
$i >= 1 || $found_first_diff) {
   
$found_first_diff = true;
   
$timespan_string .= pluralize($i, 'minute').' ';
  }
  if(
$found_first_diff) {
   
$timespan_string .= 'and ';
  }
 
$timespan_string .= pluralize($s, 'second');
  return
$timespan_string;
}

function
pluralize( $count, $text )
{
  return
$count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
?>
toreskobba at gmail dot com 10-Nov-2011 03:24
When using datediff make sure your time zone is correct, for me on Windows 7 64 bit it behaved very strange when timezone was wrong (I was comparing now against time in database and exif metadata in photos). For example: date_default_timezone_set('Europe/Oslo');
astagl at gmail dot com 09-Nov-2011 08:21
I needed to get the exact number of days between 2 dates and was relying on the this diff function, but found that I was getting a peculiar result with:

<?php
    $today
= new DateTime(date('2011-11-09'));
   
$appt  = new DateTime(date('2011-12-09'));
   
$days_until_appt = $appt->diff($today)->d;
?>

This was returning 0 because it was exactly one month.

I had to end up using :

<?php
    $days_until_appt
= $appt->diff($today)->days;
?>

to get 30.
Anonymous 03-Oct-2011 08:12
$dateTime = new DateTime('2011-08-01 00:00:00');
echo $dateTime->diff(new DateTime('2011-10-01 00:00:01'))->format('%m');

will return 1, instead of 2 ...
milespickens+php at gmail dot com 16-May-2011 03:00
I was looking for a way to output X number of days from a given date and didn't find exactly what I was looking for.  But I got this working.  I hope this helps you.

This will output the number of days,months, or years difference between NOW and a April 1st, 2011. 

<?php
    $date1
= new DateTime('2011-04-01');
   
$date2 = new DateTime("now");
   
$interval = $date1->diff($date2);
   
$years = $interval->format('%y');
   
$months = $interval->format('%m');
   
$days = $interval->format('%d');
    if(
$years!=0){
       
$ago = $years.' year(s) ago';
    }else{
       
$ago = ($months == 0 ? $days.' day(s) ago' : $months.' month(s) ago');
    }
    echo
$ago;
?>

If I used today, 2011-05-16 as $date1, I could return all 0's in the format.  For example....

<?php
        $date1
= new DateTime('2011-05-161');
   
$date2 = new DateTime("now");
   
$interval = $date1->diff($date2);
   
$diff = $interval->format('%y-%m-%d');
    echo
$diff//Today, this will output 0-0-0
?>
Grgoire Marchal 18-Feb-2011 02:22
Warning, there's a bug on windows platforms: the result is always 6015 days (and not 42...)

http://bugs.php.net/bug.php?id=51184
schindhelm at gmail dot com 21-Jan-2011 12:48
I found that DateTime::diff isn't as accurate as I thought. I calculated the age gap between now and a birthdate from before 1970 (unix epoch). Here's what I got:

Given today is January 21st, 2011:

<?php
date_default_timezone_set
('Europe/Berlin');

// birthdate format is YYYY-MM-DD
$birth = new DateTime('1966-01-21');
$today = new DateTime();
$diff = $birth->diff($today);
echo
$diff->format('%y'); // will output 45

$birth = new DateTime('1966-01-23');
$today = new DateTime();
$diff = $birth->diff($today);
echo
$diff->format('%y'); // will output 45 wrongly

$birth = new DateTime('1966-01-24'); // three days difference!
$today = new DateTime();
$diff = $birth->diff($today);
echo
$diff->format('%y'); // will output 44 - correct
?>

When calculating with the date() function it was more accurate (didn't use seconds/hours for comparison).

Note that 3 days may be a lot if you want to create invoices and have to check against a given age to determine if the customer is chargable for taxes and so on.

If someone also found this behaviour I'd like to hear about it - give me a quick mail at schindhelm (at) gmail (dot) com.
Thanks.
acrion at gmail dot com 13-May-2010 08:52
If you want to quickly scan through the resulting intervals, you can use the undocumented properties of DateInterval.

The function below returns a single number of years, months, days, hours, minutes or seconds between the current date and the provided date.  If the date occurs in the past (is negative/inverted), it suffixes it with 'ago'.

<?php
function pluralize( $count, $text )
{
    return
$count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}

function
ago( $datetime )
{
   
$interval = date_create('now')->diff( $datetime );
   
$suffix = ( $interval->invert ? ' ago' : '' );
    if (
$v = $interval->y >= 1 ) return pluralize( $interval->y, 'year' ) . $suffix;
    if (
$v = $interval->m >= 1 ) return pluralize( $interval->m, 'month' ) . $suffix;
    if (
$v = $interval->d >= 1 ) return pluralize( $interval->d, 'day' ) . $suffix;
    if (
$v = $interval->h >= 1 ) return pluralize( $interval->h, 'hour' ) . $suffix;
    if (
$v = $interval->i >= 1 ) return pluralize( $interval->i, 'minute' ) . $suffix;
    return
pluralize( $interval->s, 'second' ) . $suffix;
}
?>
Dennis C 09-May-2010 11:24
For those like me who don't yet have PHP 5.3 installed on their host, here's a simple alternative to get the number of days between two dates in the format '2010-3-23' or similar acceptable to strtotime().  You need PHP 5.2.

<?php
function date_diff($date1, $date2) {
   
$current = $date1;
   
$datetime2 = date_create($date2);
   
$count = 0;
    while(
date_create($current) < $datetime2){
       
$current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
       
$count++;
    }
    return
$count;
}

echo (
date_diff('2010-3-9', '2011-4-10')." days <br \>");
?>
Anonymous 04-Aug-2009 05:17
You don't need to calculate the exact difference if you just want to know what date comes earlier:

<?php

date_default_timezone_set
('Europe/Madrid');

$d1 = new DateTime('1492-01-01');
$d2 = new DateTime('1492-12-31');

var_dump($d1 < $d2);
var_dump($d1 > $d2);
var_dump($d1 == $d2);

?>

bool(true)
bool(false)
bool(false)