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

6
  • duplicate of How to find number of days between two dates using php and a couple others Commented Jul 6, 2010 at 9:52
  • @Gordon Bad choice; the accept answer there is wrong :p Commented Jul 6, 2010 at 9: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? Commented Jul 6, 2010 at 10:04
  • @Gordon Upon checking the links, yours is actually the only that's an exact duplicate. Commented Jul 6, 2010 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. Commented Jul 6, 2010 at 10:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

19 Comments

+1 for using the DateTime class.
+1 for using the DateTime class which is DST sensitive. (When using the correct time zone(s), of course.)
@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.
@Gordon I added two examples.
@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.).
|
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;
}

3 Comments

thanks this looks great, will accept your answer when it lets me!
This has the same problem as @Mark's answer; it doesn't consider daylight saving time.
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)
1

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


The following SO questions might be of some help:

More >>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.