1

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

I'm trying to compare two dates that are stored in two PHP variables. $curDate and $prevDate are stored in the format yyyy-mm-dd. To simply display them, I've been using date("n,j,Y", strtotime($prevDate)) or some other combination of the first parameters of the date function.

I want to compare the $curDate and $prevDate on the scale of days, but the trouble is, the 31st could be 1 day from the 1st so if I just look at the date in this way then I can't handle that behavior.

I've looked into the PHP Function JulianToJD() but can't just pass the SQL date, formatted thru the date() and strtotime() function.

Can someone help me figure out a clever way of comparing two dates on the scale of days while paying attention to the end of months?

Community
  • 1
  • 1
tnw
  • 13,521
  • 15
  • 70
  • 111

2 Answers2

3

You could do it two ways:

MySQL:

SELECT (TO_DAYS(curDate) - TO_DAYS(prevDate)) as Days ...

which gives you the difference in days (TO_DAYS() returns the number of days since year 0)

PHP, using DateTime:

$prev = DateTime::CreateFromFormat('Y-m-d', $prevDate);
$cur = DateTime::CreateFromFormat('Y-m-d', $curDate);

$days = $cur->diff($prev)->format('%a');
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • this looks good, however, I get an error on the first line of PHP, `$prev=....`: `Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' – tnw Jul 05 '11 at 15:15
  • Woops, sorry. take out the "new" portion. I'll edit the answer. – Marc B Jul 05 '11 at 15:17
  • +1 I don't like the MySQL way; the PHP way is nice and clean! – Eljakim Jul 05 '11 at 15:19
  • @Marc Taking out the `new` did fix that error, but I don't think this works as it should. I printed out `$prevDate` and `$curDate` which are `2011-03-12` and `2011-07-04`, respectively. The output for days is `6015`??? – tnw Jul 05 '11 at 15:23
  • @Marc as a side note, those date outputs by nature might seem ambiguous with the month/day, but they are in the Y-m-d format. – tnw Jul 05 '11 at 15:25
  • That's why I never use strtotime(). It usually works well, but on ambiguous dates it'll screw up silently and ruin your day. It's fairly rare to have a completely arbitrary date - usually you know what the format is, so it's best to use createfromformat and save yourself all the extra parsing/overhead of strtotime, plus you get far more dependable results. – Marc B Jul 05 '11 at 15:28
  • @Marc I don't *use* strtotime() at all with the dates inputted to your code. Here's the even weirder part... Any two dates I compare with your code **ALL** come out with the result `6015`. – tnw Jul 05 '11 at 15:30
  • Known bug on Windows: https://bugs.php.net/bug.php?id=51184 – Marc B Jul 05 '11 at 15:32
  • @Marc Oh, lovely. Any other routes I can go here that doesn't use MySQL? – tnw Jul 05 '11 at 15:34
  • @Marc NVM. borrible's link to another question worked. – tnw Jul 05 '11 at 15:36
  • You could format the dates to `'U'`, which gives you a standard unix timestamp. You can then convert that to days by dividing by 86400 (seconds in a day) and work off that. – Marc B Jul 05 '11 at 15:37
  • @Marc true... borrible's solution he posted is actually much simpler, thank you for your help though. – tnw Jul 05 '11 at 15:41
3

See stackoverflow.com/q/2040560/469210. I.e. calculate the diff of the dates and apply the floor function:

floor($datediff/(60*60*24));
borrible
  • 17,120
  • 7
  • 53
  • 75