41

I have got two dates in php

$date1 = 'May 3, 2012 10:38:22 GMT'

$date2 = '06 Apr 2012 07:22:21 GMT'

Then I subtract both of them

$date2 - $date1

, and get

Result:6

Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?

evan
  • 12,307
  • 7
  • 37
  • 51
Dmitry Makovetskiyd
  • 6,942
  • 32
  • 100
  • 160

7 Answers7

77

Part 1: Why is the result 6?

The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.

Part 2: How do you get it to work?

$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;

Convert as appropriate.

evan
  • 12,307
  • 7
  • 37
  • 51
  • 1
    Do I need to convert the seconds between two times..to days... isnt there a faster way – Dmitry Makovetskiyd May 06 '12 at 08:06
  • 1
    seconds in a day = 60*60*24 = 86,400 so just divide by that. – evan May 06 '12 at 08:06
  • This solution won't work for different factors like months, years, etc. It works for days, but I wouldn't use it to measure anything else. Instead, use the DateTime class with "diff" method to extract the actual days, months, years passed. See other answers for implementations that you should use (instead of seconds passed) – G M Jul 04 '22 at 19:06
  • As mentioned @GM `diff` doesn't work if it's more than a month. Dealing with time properly is exceedingly difficult - did you account for daylight savings, regional time zones, leap years, leap seconds, etc? That said, if you have a better solution that this one that is over 10 years old, please post it! I'm sure there have been some updates since then. – evan Jul 06 '22 at 12:32
23

Using DateTime and DateInterval,

$date1 = new DateTime("May 3, 2012 10:38:22 GMT");
$date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
echo $date1->diff($date2)->format("%d");
nietonfir
  • 4,797
  • 6
  • 31
  • 43
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • This will return me the difference in days..cause thats what I want..to find the date difference – Dmitry Makovetskiyd May 06 '12 at 08:07
  • 1
    if the difference is more than a month, `format("%d")` would't account for that. So it's not perfect! Use `echo $date1->diff($date2)->days;` instead. – Imtiaz Jul 07 '18 at 20:57
  • 1
    This is not the right answer, mostly cause DateInterval is a terrible object: `$date1 = new DateTime("May 3, 2012"); $date2 = new DateTime("06 Apr 2013"); $date1->diff($date2)->format("%d") // == 2` Also, `->days` is sometimes false: `(new DateInterval("P2Y100D"))->days // === false!` – Daniel Beardsley Jun 04 '20 at 17:49
  • `$diff->invert` is available for comparison (0 is positive, 1 is negative) also you can use `date_timestamp_get` to get timestamps from the DateTime objects and work with absolute seconds – Isometriq Oct 09 '22 at 15:13
14

There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....

Other way is that format both of dates in the same format then subtract....

Third way

$date1=  new DateTime("May 3, 2012 10:38:22 GMT");
$date2= new DateTime("06 Apr 2012 07:22:21 GMT");
echo $date1->diff($date2)->("%d");

forth way

$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == return sec in difference
$days = $secs / 86400;
ajmal iqbal
  • 157
  • 4
7

Most of presented solutions seems to be working, but everyone forgets about one thing: time.

Taking evan example:

$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');

$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;

When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00 (Y-m-d) and 2014-05-02 07:00:00 will be 0,xxx, not 1. You should trim time part of every date.

So it should be:

$datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
$datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));

$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;
ex3v
  • 3,518
  • 4
  • 33
  • 55
5
$todate= strtotime('May 3, 2012 10:38:22 GMT');
$fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
$calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
$days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
echo($days);

This code will find the date difference between two dates..

Here output is 27

Romancha KC
  • 1,507
  • 1
  • 13
  • 12
2
echo 'time'.$notification_time=  "2008-12-13 10:42:00";
 date_default_timezone_set('Asia/Kolkata');
 echo 'cureen'.$currenttime=date('Y-m-d H:i:s'); 
$now = new DateTime("$notification_time");
$ref = new DateTime("$currenttime");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
Naveen Kumar
  • 481
  • 5
  • 16
1

If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:

If you check http://php.net/manual/en/dateinterval.format.php

The correct way is:

 echo $date1->diff($date2)->format("%a");

For getting all days

Daniel Nieto
  • 131
  • 6