millsy007 Posted February 9, 2009 Share Posted February 9, 2009 I have an insert statement that works on a loop to insert and fill a coach schedule for a year It works to begin with as it inserts the journeys (starting at 9:00 12:00 16:00 and 22:00) but for some reason on 2009-03-08 when inserting the dates they are moved forward to 10:00. 13:00 etc and then on 2009-10-31 it goes back to an hour earlier again. Why is this, how can I stop it? Link to comment https://forums.phpfreaks.com/topic/144486-solved-insert-statement-adding-an-hour-on-datetime/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2009 Share Posted February 9, 2009 You have heard of daylight savings time? I'll guess (and I think I recall seeing it in your code in other posts) that you are using a Unix Timestamp. When a Unix Timestamp is used by the date() function, it takes into account the local time zone and any daylight savings time settings. I think you can avoid this problem by using gmdate() instead of date() Link to comment https://forums.phpfreaks.com/topic/144486-solved-insert-statement-adding-an-hour-on-datetime/#findComment-758158 Share on other sites More sharing options...
millsy007 Posted February 9, 2009 Author Share Posted February 9, 2009 I did think it could be that but didnt see how that would work, guess I was wrong I am using a FROM_UNIXTIME but I dont see where I have a date() to be able to change to gmdate()? I have: mysql_query ("INSERT INTO shuttle VALUES (" . $id . ", FROM_UNIXTIME(" . ( ( $departure_time * 3600 ) + $date_start ) . "))") or die (mysql_error()); Thanks for your help (again) I am a bit of a newbie so this is proving to be harder than I thought! Link to comment https://forums.phpfreaks.com/topic/144486-solved-insert-statement-adding-an-hour-on-datetime/#findComment-758180 Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2009 Share Posted February 9, 2009 Actually it is because your $date_start is being incremented by a fixed 86400 for each day, but on DST changes there are not 86400 seconds in a day. You might be able to get around this by using either strtotime() or mketime() to add one day to the current date that $date_start represents. You can also probably do this all in a query using a user (sql) variable and INTERVAL date math. Link to comment https://forums.phpfreaks.com/topic/144486-solved-insert-statement-adding-an-hour-on-datetime/#findComment-758186 Share on other sites More sharing options...
PFMaBiSmAd Posted February 9, 2009 Share Posted February 9, 2009 If you replace your line of code that is adding 86400 to $date_start by the following, it should work - $date_start = strtotime(date('Y-m-d H:i:s',$date_start) . '+ 1 day'); Link to comment https://forums.phpfreaks.com/topic/144486-solved-insert-statement-adding-an-hour-on-datetime/#findComment-758197 Share on other sites More sharing options...
millsy007 Posted February 9, 2009 Author Share Posted February 9, 2009 Perfect, thanks your a star Link to comment https://forums.phpfreaks.com/topic/144486-solved-insert-statement-adding-an-hour-on-datetime/#findComment-758507 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.