37

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
  • 11,847
  • 6
  • 35
  • 50
Dmitry Makovetskiyd
  • 6,774
  • 30
  • 98
  • 159

7 Answers7

74

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
  • 11,847
  • 6
  • 35
  • 50
21

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,697
  • 6
  • 29
  • 43
Shiplu Mokaddim
  • 54,465
  • 14
  • 131
  • 183
  • 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
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,398
  • 4
  • 31
  • 53
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,467
  • 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
  • 471
  • 5
  • 16
0

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
  • 121
  • 6