Jump to content

March 13 2016 DateTime weirdness


stevieontario

Recommended Posts

probably a basic problem but I've been banging my head against it for an hour and a half.

 

I'm trying to combine date and hour strings from a .csv file into a MySQL datetime variable.

 

So 2 a.m. on March 13 2016 is proving a bit of a challenge:

$date = '13-Mar-16';
$hour = 2;
$startdate = $date.' '.$hour.':00:00';
$datehour = new Datetime($startdate);
$datehour = $datehour->format('Y-m-d H:i:s');
echo 'datehour is '.$datehour;

returns

 

 

datehour is 2016-03-13 03:00:00

 How did hour 02 become 03?

 

Also, change '13-Mar-16' to '13-Mar-15' and the above code will return hour 02, i.e. the "proper" hour.

 

Any idea what I'm doing wrong here?

Link to comment
Share on other sites

thanks to both of you -- well that would explain it. 

 

Now that puts two identical datetimes into my db (which is how I discovered the issue in the first place). The creator of the .csv cares not about DST -- they just give hourly data, hour 1 through hour 24.

 

I guess I have to figure out something involving  getOffset().

Link to comment
Share on other sites

You have to be very careful about any calculations during the changes due to daylight savings to guard against tearing a hole in the space time continuum.

 

 

thanks to both of you -- well that would explain it. 

 

Now that puts two identical datetimes into my db (which is how I discovered the issue in the first place). The creator of the .csv cares not about DST -- they just give hourly data, hour 1 through hour 24.

 

I guess I have to figure out something involving  getOffset().

 
That is why you would want to use a timestamp (not a datetime) field. A timestamp is the number of seconds since Dec. 1(?), 1970. The "date" you might see in the database is simply the extrapolation of that timestamp into a representative value based on the servers timezone setting. So, if you do have a timestamp from the data, you are good, else you have no way of knowing which record is for which hour when the clocks go back. Although, if there is one record per hour (or a set number) I suspect you may be able to assume the first record was the first hour and the second was from the hour after the clocks were set back.
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.