hoogie Posted February 22, 2008 Share Posted February 22, 2008 This one doesn't make any sense. I've been using this code to take $date (a date returned from a database) and add one day to it: <?php $nextdate = date("Y-m-d H:i:s", strtotime($date)+((60*60)*24)); ?> This works - I've put probably 6 months worth of data through it with no problem. But when the date from the database is 2007-11-4, it only adds 23 hours. I get 2007-11-4 23:00:00 back instead of 2007-11-5. I've tested with many other dates including Nov. 4 from other years, and it works perfectly except for November 4, 2007. I can get around this by adding 25 hours instead of 24 (I don't use the time, just the date, so it doesn't matter), but I'm just really wondering what's going on here? Is this a bug in PHP? I'm running: XAMPP for Linux 1.6.5a PHP 5.2.5 MySQL 5.0.51 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 It's the daylight savings time adjustment! http://webexhibits.org/daylightsaving/b.html you will see the same thing for Nov. 2 of this year. Quote Link to comment Share on other sites More sharing options...
hoogie Posted February 22, 2008 Author Share Posted February 22, 2008 LOL - Thanks. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 22, 2008 Share Posted February 22, 2008 i suggest that you look into using mktime() to create times/dates. multiplying hours x number of days is asking for trouble, as you've found. maybe something like: <?php // Get this same time 6 months from now: $current_time = strtotime($date); $next_time = mktime(date("G",$current_time),intval(date("i",$current_time)),intval(date("s",$current_time)),date("n",$current_time) + 6, date("j",$current_time), date("Y",$current_time)); $nextdate = date("Y-m-d H:i:s", $next_time); ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 22, 2008 Share Posted February 22, 2008 If you use the strtotime() date/time math parameters, it works correctly - $nextdate = date("Y-m-d H:i:s", strtotime("$date + 1 day")); 2007-11-4 gives 2007-11-05 00:00:00 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 22, 2008 Share Posted February 22, 2008 I encountered something similar once. Drove me nuts until the light bulb went off. I had a good chuckle at myself that day. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 22, 2008 Share Posted February 22, 2008 If you use the strtotime() date/time math parameters, it works correctly - $nextdate = date("Y-m-d H:i:s", strtotime("$date + 1 day")); 2007-11-4 gives 2007-11-05 00:00:00 that's a good one. I'm not experienced with the nuances of strtotime() so i always fall back on mktime(). i also ran into this problem long ago while developing an employee time-tracking application. everything worked fine until one day in the spring... :-) Quote Link to comment Share on other sites More sharing options...
hoogie Posted February 22, 2008 Author Share Posted February 22, 2008 Wow, thanks everyone - this is all very useful information. I am new to the whole UNIX date and strtotime stuff so PFMaBiSmAd, your advice was especially helpful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.