• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

strtotime only adds 23 hours when passing "+1 day"

strtotime("+1 day") only adds 23 hours after changing server timezone from 'UTC' to 'Europe/Stockholm'. Tried changing php date.timezone to 'Europe/Stockholm'.

  $int_day = strtotime($_POST["start_day"]);
  $str_day = date("Y-m-d H:i:s", $int_day );
  print "today is $int_day<br>\n";
  print "that is, $str_day<br>\n";
  print "going forward..<br>\n";
  $int_day = strtotime("+1 day", $int_day);
  $str_day = date("Y-m-d H:i:s", $int_day);
  print "tomorrow is $int_day<br>\n";
  print "that is, $str_day<br>\n";


  today is 1269039600
  that is, 2010-03-20 00:00:00
  going forward..
  tomorrow is 1269126000
  that is, 2010-03-20 23:00:00

I also tried "+24 hours" instead of "+1 day" STILL only adds 23 hours. :(
Any help is very much appreciated!
  • 2
1 Solution
mgabAuthor Commented:
Update: After change to summertime (DST, Daylight Savings Time) it only adds 22 hours!

  today is 1270418400
  that is, 2010-04-05 00:00:00
  going forward..
  tomorrow is 1270504800
  that is, 2010-04-05 22:00:00
It seems every thing ok with you code. maybe you have not put actual code here.
but I think following info can help you to understand the issue.

1. there is two hour difference between UTC and StockHolm time

2. date saved in unix timestamp (1270418400 etc..) represent UTC timezone
3. your system timezone settings does not affect timestamp, it is always UTC.
4. while doing conversion between timestamp and date. like  date("Y-m-d H:i:s", $int_day); or strtotime("+1 day", $int_day);
 here make sure that timezone settings are same.
mgabAuthor Commented:
That was the actual code.

After debugging, it seems that date() generates a utc-time but when doing a strtotime("+1 day") it is timezone-aware and only adds the amount of time that would correspond to the current timezone.

I've played a little with gmdate() but with no success, so I just reverted the timezone back for this little script:

      // set default timezone to utc since strtotime() and date() doesn't play along very well

NOTE: This was running PHP 5.2.6-1+lenny8.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now