96

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?

trejder
  • 17,148
  • 27
  • 124
  • 216
mysqllearner
  • 13,523
  • 15
  • 43
  • 43
  • @kiamlaluno - that's not an *exact* duplicate, since it's a good deal more complicated (the OP in that question wants date formatted as in "x years, y months, z days" etc). – Dominic Rodger Dec 21 '09 at 14:32

3 Answers3

209

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

Community
  • 1
  • 1
adam
  • 2,264
  • 2
  • 13
  • 6
  • 3
    This function requires php 5.3 or better as stated by @txyoji – levhita Oct 02 '12 at 22:45
  • 20
    use `$diffInDays = (int)$dDiff->format("%r%a");` to get values you can directly use such as "-2" or "2" (see http://www.php.net/manual/en/dateinterval.format.php for format parameter descriptions) – flu Oct 29 '12 at 13:36
  • And change "-" to ":" if you accidentaly or not trying to put date with time... – webrama.pl May 26 '13 at 16:57
  • 1
    Why would you put `date()` function as first parameter of DateTime constructor? Function call `date('2012-07-26')` will return `2012-07-26`, so I really see no point of calling that function. – Glavić Aug 23 '13 at 07:46
  • `days . " days ";` Output: difference 6015 days How can we get zero days for same date? – Naresh Kumar Nakka May 30 '16 at 13:44
55

strtotime will convert your date string to a unix time stamp. (seconds since the unix epoch.

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);

$seconds_diff = $ts2 - $ts1;
Philip Morton
  • 129,733
  • 38
  • 88
  • 97
txyoji
  • 6,680
  • 1
  • 44
  • 46
  • 11
    please note that this might give you different results if between the 2 dates there is a time change (like in spring or autumn). try this: echo strtotime('2013-03-30 21:00') - strtotime('2013-03-29 21:00'); echo strtotime('2013-03-31 21:00') - strtotime('2013-03-30 21:00'); you get 86400 and 82800. the results are actually correct. but if you divide by 86400 to get the number of days you need to take that into account (so better use the date diff function). – memical Mar 29 '13 at 11:07
  • 2
    If you're on a modern version of php, use adam's solution. PHP has a lot of code to account for anomalies related to time. – txyoji Sep 22 '14 at 19:12
22

Below code will give the output for number of days, by taking out the difference between two dates..

$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Anirudh Sood
  • 1,418
  • 12
  • 6
  • When trying to calculate date difference for my `JQuery datepicker`, `floor` was giving me strange result. By example, `floor((strtotime(((isset($_POST['date']) and strtotime($_POST['date'])) ? $_POST['date'] : date("Y-m-d"))) - strtotime(date("Y-m-d")))/3600/24))` give me a difference of 9.9583333333333 between March 31st and March 21st (so 9 with rounded down when I'm expecting 10). I used the `round` function to solve the problem. – Jonathan Parent Lévesque Mar 21 '16 at 18:44
  • Yes Jonathan, You are right. – Anirudh Sood Sep 13 '16 at 08:07
  • Hey Bro I have a doubt, can You explain what is the use of 3600/24 in your echo – Siva Ganesh Apr 03 '18 at 09:11
  • 1
    @SivaGanesh he's making the timestamp in hours (/3600) and then in days (/24) – napolux Jan 23 '19 at 18:57