tags:

views:

111

answers:

3

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

+11  A: 
$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
+1 for using the [DateTime class](http://www.php.net/manual/en/class.datetime.php).
Mike
+1 for using the DateTime class which is DST sensitive. (When using the correct time zone(s), of course.)
Pekka
Only php >= 5.2 though :)
Bogdan Constantinescu
@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
RHEL/CentOS and all business distros use 5.1.6 (at the moment)
Bogdan Constantinescu
@Bog I mean they're not supported upstream, granted some distributions backport bug fixes.
Artefacto
@Bogdan Constantinescu,the limit version is 5.3 or higher.
SpawnCxy
@Spawn You're right, there was no DateTime::diff in 5.2. I added a note.
Artefacto
+1 for the <5.2 method with reference to testing the neighbourhood
Mark Baker
@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
@Gordon I added two examples.
Artefacto
@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
@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
@Artefacto yepp, I am happy now. With that added, I can upvote. Enjoy your Nice Answer badge.
Gordon
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
@Har `$days = $diff->format("%a"); $in_the_past = ($days < 0);`
Artefacto
@Artefacto - your original example keeps returning 6015? I'm using php 3.50 on WAMP
Haroldo
@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
@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
@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
+1  A: 

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
thanks this looks great, will accept your answer when it lets me!
Haroldo
This has the same problem as @Mark's answer; it doesn't consider daylight saving time.
Artefacto
DateTime class is only php 5.2 or higher. I can show you all major stable business distros use php 5.1 (take for example RHEL/CentOS)
Bogdan Constantinescu