1

Why does this sum only 4 days when using 25-Oct-13 and 5 days (as expected) when using other dates?

<?php
    echo date('d-M-y',(strtotime('25-Oct-13') + (432000)));
?>
Traian Tatic
  • 681
  • 5
  • 18

2 Answers2

1

This depends on your timezone. The last Saturday of October is the end of Daylight Saving Time (DST) in some locales. Therefore the night of October 26th to 27th in 2013 may or may not contain an extra hour.

Circumvent this issue by adding actual days instead of hours:

$myDate = new \Datetime('2013-10-25');
$myDate->add(new \DateInterval('P5D'));

This does return Oct 30th 2013.

More strange stuff can happen from incorrectly assuming that days are always exactly 24 hours.

Community
  • 1
  • 1
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • I've added this: date_default_timezone_set('America/Adak'); It works fine now. Thank you! – Traian Tatic Aug 27 '14 at 11:29
  • Changing the timezone is not a fundamental solution to your problem. – Niels Keurentjes Aug 27 '14 at 11:30
  • I don't understand the meaning of 'fundamental solution' but as long as it won't cause me any trouble it's ideal. It's the only operation I'm doing with those dates; no other details will be used/ added, ever. – Traian Tatic Aug 27 '14 at 11:45
  • The point of a fundamental solution is that it *will not* cause you any trouble. Changing the timezone to a region that has another DST rule is a recipe for disaster when changing other dates, or when they do introduce DST in Adak. The fundamental solution is to calculate with days, not hours, as stated in my answer. – Niels Keurentjes Aug 27 '14 at 11:46
  • 1
    Done it your way. Works (probably you already knew that xD). Thank you! – Traian Tatic Aug 27 '14 at 11:58
1

$date = "2014-09-17";

echo date('Y-m-d', strtotime($date.' + 5 days'));

Kiran
  • 121
  • 6