0

I implemented a countdown in one of our projects. As this was my first countdown I just realized that if I tell PHP:

$timeleft = strtotime('2013-10-31') - strtotime('now');
$daysleft = floor( $timeleft/(24*60*60) );

I get the time left to 2013-10-31 00:00 instead of the full day.

Why is it this way? I expected 2013-10-31 23:59:59 or 2013-11-01 00:00

As an example from daily life: I have a rental contract for a car until 2013-10-31, or any contract, then I know that the rental period always includes the final day completely.

This might seem like a beginner's question but I would like to know the reason for this decision. Thank you for your ideas and experience.


PS: Is it the same in all programming languages?

3 Answers 3

2

Then give a time to strtotime as well.

$timeleft = strtotime('2013-10-31 23:59:59') - strtotime('now');

Why?

Because when you say:

strtotime('2013-10-31');

That means the exact timestamp when that date started, which is equivalent to saying:

strtotime('2013-10-31 00:00:00');  // hence the missing 24 hours

Edit

In fact, saying strtotime('now') is just like saying time(), so you can remove that as well. And have it like:

$timeleft = strtotime('2013-10-31 23:59:59') - time();

PS: Is it the same in all programming languages?

Its not a language limitation by any means, in any language. Its how you interpreted the function which generates time for you. After interpreting it correctly, send it the right values and it will work as you expect.

3
  • Is there another way of stating the final day which will include the 24 hours? For now I am using $timeleft = strtotime('2013-10-31 + 1 day') - strtotime('now');
    – Avatar
    Nov 1, 2013 at 11:01
  • +1 day is also fine, because start of next day will be equal to end of this day. You can also use php.net/mktime Nov 1, 2013 at 11:03
  • One note why I am not using time(), I specify date_default_timezone_set('Europe/Vilnius'); before to receive the correct local time. Using time() I would get the UTC which is not the same :)
    – Avatar
    Nov 1, 2013 at 11:23
1

The reason for this will be the same as it is for money: 1 EURO = 1.00 EURO and not 1.50 EURO.

If the time of day matters, you should define it in both objects.

1

As dates can be ambiguous, varying on time zones, daylight savings - even the architecture of the system that its running on, it is recommended to use the built in DateTime object.

There are numerous methods that can be used to manipulate the date, such as DateTime::diff() without all the manual calculations (ie. floor( $timeleft/(24*60*60) );).

So they might offer you better flexibility and accuracy when comparing date intervals and/or adding them together.

$date = new DateTime('2013-10-31');
$now  = new DateTime('today');
$diff = $date->diff($now);
$new  = $date->add($diff);

echo $new->format('Y-m-d H:i'); // 2013-11-01 00:00
3
  • How is this cleaner than $timeleft = strtotime('2013-10-31 23:59:59') - time(); Nov 1, 2013 at 11:12
  • @Hanky웃Panky I've clarified what I meant, don't get me wrong, of course the built in date methods can be used, however, I often want to use dates more than once, where having to manually calculate them each time is repetitive and error prone
    – AlexP
    Nov 1, 2013 at 11:26
  • Cheers, good answer. I only meant point so that the word cleaner is replaced with better Nov 1, 2013 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.