1

Possible Duplicate:
How to find number of days between two dates using php

Is there a quick and easy way to calculate the difference in days between two date strings in this format (YYYY-MM-DD) with PHP (not MySQL)?

Community
  • 1
  • 1
Haroldo
  • 36,607
  • 46
  • 127
  • 169
  • duplicate of [How to find number of days between two dates using php](http://stackoverflow.com/questions/2040560/how-to-find-number-of-days-between-two-dates-using-php) and a couple others – Gordon Jul 06 '10 at 09:52
  • @Gordon Bad choice; the accept answer there is wrong :p – Artefacto Jul 06 '10 at 09:56
  • @Artefacto Feel free to suggest another. But there is at least five other links to similar questions among the answers to the question I linked as duplicate. Linking to a question doesn't imply having to use the accepted answer does it? – Gordon Jul 06 '10 at 10:04
  • @Gordon Upon checking the links, yours is actually the only that's an exact duplicate. – Artefacto Jul 06 '10 at 10:10
  • @Artefacto just believe me, this has been asked and answered before many times :) I am just too lazy to do the OP's work and find a better suited one. Like I said, feel free to find one more fitting. – Gordon Jul 06 '10 at 10:16
  • *(useful)* [O'Reilly PHP Cookbook - Finding the Difference of Two Dates](http://docstore.mik.ua/orelly/webprog/pcook/ch03_06.htm) – Gordon Jul 06 '10 at 10:32

3 Answers3

16
$date1 = new DateTime("2010-07-06"); //inclusive
$date2 = new DateTime("2010-07-09"); //exclusive
$diff = $date2->diff($date1);
echo $diff->format("%a"); //3

(PHP 5.3 and higher only)

The only solution I see for PHP < 5.2 is to loop:

strtotime("-1 days");
strtotime("-2 days");
...
strtotime("-n days");

until we get to the unix timestamp of the first date. That's conceptually, you can do it in a much more efficient way, by first guessing the number of days with the timestamp difference of the two days and then testing the neighborhood.

Why dividing by 86400 doesn't work

date_default_timezone_set("Europe/Lisbon");
$date1 = strtotime("2010-03-28");
$date2 = strtotime("2010-03-29");
echo ($date2-$date1)/86400; //gives 0.95833333333333
$date1 = strtotime("2010-10-31");
$date2 = strtotime("2010-11-01");
echo ($date2-$date1)/86400; //gives 1.0416666666667

As Gordon correctly has pointed out, dividing by 86400 would be a valid solution for this problem if the timezone was set to 'UTC' before – just don't forget to restore it to the previous value after.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • 2
    +1 for using the [DateTime class](http://www.php.net/manual/en/class.datetime.php). – Mike Jul 06 '10 at 09:58
  • +1 for using the DateTime class which is DST sensitive. (When using the correct time zone(s), of course.) – Pekka Jul 06 '10 at 09:59
  • @Bog Granted, but PHP 5.1 and lower are not supported anymore; people shouldn't be using them – not that it stops them, of course. – Artefacto Jul 06 '10 at 10:02
  • RHEL/CentOS and all business distros use 5.1.6 (at the moment) – Bogdan Constantinescu Jul 06 '10 at 10:04
  • @Bog I mean they're not supported upstream, granted some distributions backport bug fixes. – Artefacto Jul 06 '10 at 10:06
  • @Bogdan Constantinescu,the limit version is 5.3 or higher. – Young Jul 06 '10 at 10:07
  • @Spawn You're right, there was no DateTime::diff in 5.2. I added a note. – Artefacto Jul 06 '10 at 10:12
  • +1 for the <5.2 method with reference to testing the neighbourhood – Mark Baker Jul 06 '10 at 11:06
  • @Artefacto as much as I agree to using DateTime, as much do I think it would be helpful for others to show them an Example when DST actually affects the outcome. – Gordon Jul 06 '10 at 11:11
  • 1
    @Gordon I added two examples. – Artefacto Jul 06 '10 at 11:24
  • @Artefacto thanks, though it should be noted that this could easily be solved with setting `date_default_timezone_set('UTC');` which doesnt know DST. The PEAR_Date package does it this way too I think. – Gordon Jul 06 '10 at 11:28
  • 1
    @Gordon That's a good idea, and it actually solves this particular problem. It only starts to fall apart in cases where the timezone is significant (e.g. calculating future times, difference between two arbitrary times, etc.). – Artefacto Jul 06 '10 at 11:37
  • @Artefacto yepp, I am happy now. With that added, I can upvote. Enjoy your Nice Answer badge. – Gordon Jul 06 '10 at 11:44
  • Thanks for this guys, how can i deal with minus numbers with this solution - or more specifically, how can i get __$in_the_past = TRUE;__ ? – Haroldo Jul 06 '10 at 12:16
  • @Har `$days = $diff->format("%a"); $in_the_past = ($days < 0);` – Artefacto Jul 06 '10 at 12:40
  • @Artefacto - your original example keeps returning 6015? I'm using php 3.50 on WAMP – Haroldo Jul 06 '10 at 12:57
  • @Har I don't know what you're doing wrong... It does return `3`. See http://codepad.viper-7.com/TTIrAR BTW, I think you meant PHP 5.3. – Artefacto Jul 06 '10 at 13:34
  • @Artefacto even if i copy your exact code into a new php file i still get '6015' - i notice that the tester you were using (looks like a really useful tool) uses 5.3dev could this be sometihing to do with it? – Haroldo Jul 06 '10 at 14:35
  • @Har I've no idea what's happening. Try updating to the latest version of PHP and if you're still getting differences of over 16 years submit a bug report. – Artefacto Jul 06 '10 at 21:15
2

You can use this function to get the number of days between two date("Y-m-d H:i:s"):

function dateDiff($dateStart, $dateEnd) 
{
    $start = strtotime($dateStart);
    $end = strtotime($dateEnd);
    $days = $end - $start;
    $days = ceil($days/86400);
    return $days;
}
Bogdan Constantinescu
  • 5,296
  • 4
  • 39
  • 50
1

Copied from the Duplicate I've linked below the question.


The following SO questions might be of some help:

More >>

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559