开发者

PHP date function not accepting timezone change

I've been trying to understand and debug timezones between MySQL and PHP.

The dates are being created in MySQL using NOW() in a date开发者_Go百科time field. The initial problem I was trying to figure out is what timezone MySQL is using and how to sync it up with PHP. But now there's an even weirder issue.

In my test app, if you change your timezone, the first strtotime updates properly, but if you pass it into a date() function it doesn't change. This is how I'm setting the timezone based off the select box.

    $current_timezone = 'America/New_York';
if( isset( $_GET['timezone'] ) ) $current_timezone = $_GET['timezone'];

date_default_timezone_set($current_timezone);

Any ideas as to why the date() function isn't accepting the timezone changes?


The date() function is operating correctly.

the default timezone also affects strtotime.

Notice that when you change the timezone, the only column in your table that changes is the timestamp column.

You need to determine (or set) the timezone that mysql is operating in. I would recommend UTC.

Then try one of the following:

  1. When you pull a date from mysql, either date_default_timezone_set('UTC') before calling strtotime(), and then set it back to the user preference before calling date
  2. Try adding a timezone part to the date string you get back from mysql:

    //since we added a timezone part, this will be 20:14:01 in UTC. $actualTime = strtotime($strDate);

Here's a little demo script that might help elucidate:

<?PHP
date_default_timezone_set('America/New_York');
$date = '2010-05-22 20:14:01'; //assume it's from mySQL, which is operating in UTC

echo strtotime($date) . ' ' . date('H:i:s', strtotime($date));
echo "\n";
echo strtotime($date.' -0000') . ' ' . date('H:i:s', strtotime($date . ' -0000'));
echo "\n";

Output looks like:

1274573641 20:14:01
1274559241 16:14:01

The first line has strtotime operating in America/New_York (since that's the default).

The second line has an explicit timezone set in the input to strtotime.


Try this. Its work for me.

<?php
$time=39600;
$datetime = strtotime(date("h:i:s A"))+$time;
$datetime = date('D, d M', $datetime);
echo $datetime;

// $time obtained from table below:
//  -25200|International Date Line (West) GMT-12|
//  -21600|Midway Island, Samoa GMT-11|
//  -18000|Hawaii, Honolulu GMT-10|
//  -14400|Alaska GMT-9|
//  -10800|Pacific Standard Time, US, Canada GMT-8|
//  -7200|British Columbia N.E., Santa Fe, Mountain Time GMT-7|
//  -3600|Central America, Chicago, Guatamala, Mexico City GMT-6|
//  0|US, Canada, Bogota, Boston, New York GMT-5|
//  +3600|Canada, Santiago, Atlantic Standard Time GMT-4|
//  +7200|Brazilia, Buenos Aires, Georgetown, Greenland GMT-3|
//  +10800|Mid-Atlantic GMT-2|
//  +14400|Azores, Cape Verde Is., Western Africa Time GMT-1|
//  +18000|London, Iceland, Ireland, Morocco, Portugal GMT|
//  +21600|Amsterdam, Berlin, Bern, Madrid, Paris, Rome, GMT+1|
//  +25200|Athens, Cairo, Cape Town, Finland, Greece, Israel GMT+2|
//  +28800|Ankara, Aden, Baghdad, Beruit, Kuwait, Moscow GMT+3|
//  +32400|Abu Dhabi, Baku, Kabul, Tehran, Tbilisi, Volgograd GMT+4|
//  +36000|Calcutta, Colombo, Islamabad, Madras, New Dehli GMT+5|
//  +39600|Almaty, Dhakar, Kathmandu, Colombo, Sri Lanka GMT+6|
//  +43200|Bangkok, Hanoi, Jakarta, Phnom Penh, Australia GMT+7|
//  +46800|Taipei, Beijing, Hong Kong, Singapore, GMT+8|
//  +50400|Seoul, Tokyo, Central Australia GMT+9|
//  +54000|Brisbane, Canberra, Guam, Melbourne, Sydney, GMT+10|
//  +57600|Magadan, New Caledonia, Solomon Is. GMT+11|
//  +61200|Auckland, Fiji, Kamchatka, Marshall, Wellington, GMT+12|
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜