Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Difference in php on days? [duplicate]

Tags:

date

php

datediff

Is there a quick way to calculate date difference in php? For example:

$date1 = '2009-11-12 12:09:08'; $date2 = '2009-12-01 08:20:11'; 

And then do a calculation, $date2 minus $date1

I read php.net documentation, but no luck. Is there a quick way to do it?

like image 575
mysqllearner Avatar asked Dec 21 '09 14:12

mysqllearner


People also ask

How can calculate date difference in days in php?

The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.

How can I compare two dates in php?

Once you have created your DateTime objects, you can also call the diff() method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object. $last = new DateTime( "25 Dec 2020" ); $now = new DateTime( "now" );


1 Answers

I would recommend to use date->diff function, as in example below:

   $dStart = new DateTime('2012-07-26');    $dEnd  = new DateTime('2012-08-26');    $dDiff = $dStart->diff($dEnd);    echo $dDiff->format('%r%a'); // use for point out relation: smaller/greater 

see http://www.php.net/manual/en/datetime.diff.php

like image 161
adam Avatar answered Oct 17 '22 02:10

adam