views:

392

answers:

3

Hey,

I am using DATETIME as a column type and using NOW() to insert. When it prints out, it is three hours behind. What can I do so it works three hours ahead to EST time?

I am using php date to format the DATETIME on my page.

Thanks,

Ryan

+3  A: 

If the date stored in your database by using NOW() is incorrect, then you need to change your MySQL server settings to the correct timezone. If it's only incorrect once you print it, you need to modify your php script to use the correct timezone.

Edit:

Refer to W3schools' convenient php date overview for information on how to format the date using date().

Edit 2:

Either you get GoDaddy to change the setting (doubtful), or you add 3 hours when you insert into the table. Refer to the MySQL date add function to modify your date when you set it in the table. Something like date_add(now(), interval 3 hour) should work.

Your exact problem is described here.

Rahul
If i am using DATETIME then how should I insert the current date?Thanks,Ryan
Coughlin
You will need to use date(), the standard php function for formatting dates, and you will need to correct your timezone settings in php using the link I posted.
Rahul
I know how to use date() I have that, displaying that date is fine. It is just going in to my database three hours behind. Any idea what I can do? I added:date_default_timezone_set('America/New_York');
Coughlin
I looked at my server variables and under TIME_ZONE it was set to "SYSTEM"
Coughlin
If it's set to system then that implies that MySQL is using the same timezone setting as the machine it's hosted on. What's the timezone setting for the machine hosting the MySQL server?
Rahul
It is Pacific, I am using GoDaddy and they are located out on the West Coast.Thanks,Ryan
Coughlin
There's your problem then :-) Let me edit my answer with some more info.
Rahul
A: 

Give gmdate() and gmmktime() a look. I find timestamp arithmetic much easier if you use GMT, especially if your code runs on multiple machines, or modifying MySQL server settings isn't an option, or you end up dealing with different timezones, day light savings, etc.

A: 

I would suggest inserting the date in UTC time zone. This will save you a lot of headache in the future (Daylight saving problems etc...)

"INSERT INTO abc_table (registrationtime) VALUES (UTC_TIMESTAMP())"

When I query my data I use the following PHP script

<?  while($row = mysql_fetch_array($registration)){ 

  $dt_obj = new DateTime($row['message_sent_timestamp']." UTC");
  $dt_obj->setTimezone(new DateTimeZone('Europe/Istanbul'));
  echo $formatted_date_long=date_format($dt_obj, 'Y-m-d H:i:s'); } ?>

You can replace the datetimezone value with one of the available php timezones here:

Haluk