0

I am trying to convert $dt = "Tue May 15 00:09:06 UTC+0100 2012" into a datetime field of MySQL database server, but the entered value is always 6 hours behind.

My server is in Dallas, US and I am in UK.

Any ideas how to resolve this?

2
  • Seems like you just answered your own question. The database is most likely returning the date in the local timezone. Commented May 1, 2012 at 17:14
  • "Any ideas how to resolve this?" -- add 6 hours to each datetime from the db? Commented May 1, 2012 at 17:16

2 Answers 2

1

use

 date_default_timezone_set("America/New_York");

replacing "America/New_York" to the code for London to shift your servers timezone to the proper GMT timezone. You can look up the proper code on php.net if you look for timezone stuff.

Just place it at the top of each page that you use dates and then use php DateTime objects with the ->format('c') function.

You can also set timezone information in your php.ini file if you have access to it.

-Jordan

Sign up to request clarification or add additional context in comments.

1 Comment

I wouldn't recommend this option as it will change the date displayed to end users to GMT, instead of local time. Yes, it's less work now, but if you ever decide that you want to display a different timezone by default, or allow users to override the setting, this will cause more of a headache than it's worth.
0

Without more information (some code, for example), it's difficult to debug, but given the variable you did provide, this snippet may work:

<?php
$dt_for_db = gmdate("Y-m-d H:i:s", strtotime($dt));

Then, every time you pull from the database, be sure to append GMT (there's a space at the beginning) to every date.

<?php
$dt_from_db = $dt_from_db.' GMT';

Basically, dates in MySQL datetime columns do not store timezone information, so you have to be sure to always know what timezone you're using (UTC/GMT). Then, append that timezone to the end of each date you pull from the database before using any date functions on it (as date functions assume local time for dates without a timezone specified).

9 Comments

this is the datetime i am passing "Tue May 1 20:46:43 UTC+0100 2012". and this is what is inserted into database table "2012-05-01 19:46:43" code i am using os here gmdate('Y-m-d H:i:s O', strtotime($_POST["hdn_step3_date_time"]));
@JagjotSingh, I'd suggest formatting the date before inserting it into the database, so you have full control over what is inserted (first snippet in my answer).
@JagjotSingh, $dt_from_db is one hour behind? Or the date inserted into the database is one hour behind?
@JagjotSingh, would that be because of your +0100 (+1 hour offset)?
I tried to change this value "17-May-2012 @ 23:49". Using your code gmdate("Y-m-d H:i:s", strtotime($_POST["hdn_step2a_date_time"])); like this and this is what i got "17-May-2012 22:49:00". Its 1 hours less.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.