Home » Php » Subtracting two dates in php

Subtracting two dates in php

Posted by: admin April 23, 2020 Leave a comment

Questions:

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 ?

How to&Answers:

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.

Answer:

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

Answer:

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;

Answer:

$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

Answer:

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;

Answer:

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

Answer:

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