Retrieved date, time off by 7 hours?

2019-08-08 19:57发布

问题:

I am having trouble displaying the correct date, well actually its only the time.

In the db the date is stored in this form: '2012-09-28 23:30:00' (off by 7 hours of actual date time)

That is the value for a date of an event.

When I retrieve it using this script(for drupal): $ttdate = $obj->field_date_test_value; $nttdate = strtotime($ttdate); $okdate = format_date($nttdate, $type = 'medium'); print $okdate;

...it works fine but the time is off by 7 hours. So instead of showing 'Fri, 9/28/2012 - 4:30pm' it shows 'Fri, 9/28/2012 - 11:30pm'.

Note that on the event's page, drupal retrieves the correct time...

I did some research and I figured it has to do with the timezone? But I have the timezone set to Los Angeles so I am not quite sure what is going on. Maybe its my script?

回答1:

I assume that you use the Date module.

This is probably because the Date module saves the date in UTC and alters the output on rendering (using format_date(), I think). Since you do not format the date in your own code, you see the raw UTC time (which is PDT + 7 hours) even though the correct date is displayed by the event page.

The bottom of this page explains how timezones are handled in the Date module. My guess is that either the first or third scenario is relevant in your case.



回答2:

Try the following in mysql:

set time_zone = '-07:00';