Simmo Posted October 11, 2010 Share Posted October 11, 2010 Hi I use strtotime to add 7 dates into my database, so if the user selects a weeks holiday I can see what the next 6 days are and store them in a table. This was working well, so I thought. Until I put the script on another server. For some reason it wouldn't increment from Oct 30 2010 to Nov 5, it would go 31, 01, 01, 01, 01, 01. I asked them to update PHP which they did, this didn't make any difference. Though I am in England I asked them to change the time zone to that of my testing server which was Chicago. This then worked for Oct 2010, but made me worry that there could be a problem, so I ran the following script to see how the dates would look in October for the next 5 years: <?phpecho "Date Test";echo "<br />";for($k=10; $k<16; $k++){for($j=01; $j<13; $j++){ $varDate = "29-".$j."-20".$k.""; for($i=1; $i<7; $i++){ $varDate = date('Y-m-d',(strtotime($varDate) + 1*24*3600)); echo $varDate."<br />"; } echo "<br />";}}?> On both servers there is now problem in October 2014 and 2015, here are the results of those month’s: 2014-10-30 2014-10-31 2014-11-01 2014-11-02 2014-11-02 2014-11-02 and 2015-10-30 2015-10-31 2015-11-01 2015-11-01 2015-11-01 2015-11-01 Any help would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/ Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 Hmmm... In the United States, what happens the first Sunday in November? You will be better off using PHP to calculate dates instead of using your own addition. PHP date/time functions take timezones, leap years and other differences (like what I am hinting at) into account. $varDate = date('Y-m-d', strtotime('+1 day', strtotime($varDate))); Or something similar, or mktime(). Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121180 Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 You can also try: date_default_timezone_set('UTC'); //or date_default_timezone_set('GMT'); Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121183 Share on other sites More sharing options...
litebearer Posted October 11, 2010 Share Posted October 11, 2010 or... $date = "2014-10-31"; $i=1; while($i< { echo $date . "<br>"; $date =date("Y-m-d", (strtotime($date) + 86400)); $i++; } (the math works ie http://nstoia.com/myday.php) Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121185 Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 Only because your server is set to a timezone that doesn't observe DST, such as GMT or UTC, etc... Which is my point about the math. echo date_default_timezone_get()."\n"; $date = "2014-10-31"; $i=1; while($i< { echo $date . "\n"; $date =date("Y-m-d", (strtotime($date) + 86400)); $i++; } America/Chicago 2014-10-31 2014-11-01 2014-11-02 2014-11-02 2014-11-02 2014-11-02 2014-11-02 date_default_timezone_set('UTC'); echo date_default_timezone_get()."\n"; $date = "2014-10-31"; $i=1; while($i< { echo $date . "\n"; $date =date("Y-m-d", (strtotime($date) + 86400)); $i++; } UTC 2014-10-31 2014-11-01 2014-11-02 2014-11-03 2014-11-04 2014-11-05 2014-11-06 Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121187 Share on other sites More sharing options...
litebearer Posted October 11, 2010 Share Posted October 11, 2010 Point taken Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121193 Share on other sites More sharing options...
PFMaBiSmAd Posted October 11, 2010 Share Posted October 11, 2010 Actually, the errors being shown in the results are likely due to incorrect values in the time-zone/dst database, rather than the fact that the time zone is one where dst affects the result (there would at most be a 1 hour error when the day changes, not the same result for several days.) Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121199 Share on other sites More sharing options...
AbraCadaver Posted October 11, 2010 Share Posted October 11, 2010 Actually, the errors being shown in the results are likely due to incorrect values in the time-zone/dst database, rather than the fact that the time zone is one where dst affects the result (there would at most be a 1 hour error when the day changes, not the same result for several days.) No, if the system subtracts an hour and then you add 24 then you have the same day. Do it again and you have the same day, and so on. Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121203 Share on other sites More sharing options...
Simmo Posted October 11, 2010 Author Share Posted October 11, 2010 Thanks for your comments. I should have started with the basics first and used the +1 day, I seemed to have over complicated things. All solutions have been great, adding date_default_timezone_set('UTC'); sorted the problem out straight away, so I'll use that. It’s been interesting to see how time zone settings need to be considered. I have also learned that the first Sunday in November is when the American clocks change. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/215636-strtotime-doesnt-like-october/#findComment-1121233 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.