Jump to content

[SOLVED] Insert Statement Adding an Hour on Datetime


millsy007

Recommended Posts

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?

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()

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.