0

Possible Duplicates:
Difference between dates
How to calculate the date difference between 2 dates using php

So, I have two dates. For instance, 2010-09-24 and 2010-09-25. I want to check if between those two dates the difference is 1 day.

I don't need to get a value of 86400 which means one day in seconds.

Don't forget that it can be 28, 28, 29, 30, 31 days in month.

Thanks.

What I have right now, but it doesn't work when there is difference between months:

$strto = strtotime($date);
$d1 = date('d', $strto);
$d2 = date('d', time());
echo $d2- $d1;
Community
  • 1
  • 1
good_evening
  • 21,085
  • 65
  • 193
  • 298

4 Answers4

1

Why are you using date('d'... which returns the day of the month?

strtotime will create a UNIX-timestamp which is exactly what time() returns, so abs(time() - strtotime($date)) should already do the job. This way you don't have to worry how many days a month has as you're only working with timestamps.

This will get you the number of (complete) days:

floor( abs(time() - strtotime($date)) / 86400 )
Select0r
  • 12,234
  • 11
  • 45
  • 68
  • No it won't do the job, because `$date` and `time()` can have only, for example, 5 seconds. – good_evening Oct 05 '10 at 15:27
  • Then I didn't get your question as well as what you mean by "can have only 5 seconds" ... how can `time()` "have 5 seconds" ??? If the return value is bothering you, use `floor(value/86400)` to get the number of days instead of seconds. – Select0r Oct 05 '10 at 15:29
  • I think I have misunderstood the strtotime function. – good_evening Oct 05 '10 at 15:33
1

Don't use the day value - (eg date('d', ...)) - leave it as an integer (the result of strtotime()). Then subtract those dates, and then get the floor(difference / 86400).

Like so:

$dt = strtotime($date);
echo floor(abs(time() - $dt) / 86400);
mway
  • 4,334
  • 3
  • 26
  • 37
1

You can use strtotime to get the number of seconds in between

echo abs(strtotime('2010-09-24') - strtotime('2010-09-25'));
Galen
  • 29,976
  • 9
  • 71
  • 89
  • Just watch out in case the date range encompasses the change for daylight savings, because then your seconds result won't be a whole number of days – Mark Baker Oct 05 '10 at 15:46
  • @Mark Baker : What do you mean? – good_evening Oct 05 '10 at 15:47
  • 1
    echo abs(strtotime('2010-09-24') - strtotime('2010-09-25')); would normally return 86400; but if the daylight savings began or ended on '2010-09-24', then you'd get a return value of 82800 or 90000 depending on whether the clocks were going forward or back (assumption of 1hr and a timezone that implements daylight saving). To avoid problems, you can set date_default_timezone_set('GMT'); before doing the calculation, and set it back again afterwards, because GMT is constant through the year (no daylight saving). – Mark Baker Oct 05 '10 at 15:55
  • @Mark Baker: Thank you. I'll surely do. – good_evening Oct 05 '10 at 18:51
1

You can do this nicely with the DateTime class if you have PHP 5.3:

<?php

$datetime1 = new DateTime('2010-09-25');
$datetime2 = new DateTime('2010-09-26');

$interval = $datetime1->diff($datetime2);

$intervaldays = (int) $interval->format('%R%d'); // %R signs the result +/-

This is probably less efficient than using strtotime methods, but it is very readable.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318