简体   繁体   中英

Can I show date in local time zone while save date in UTC?

我想在本地时区显示日期,但是我已经在UTC中保存了日期,因此可以使用php在本地时区显示日期。

PHP's strtotime function will interpret timezone codes, like UTC. If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it.

Assuming you get the date with timestamp code (like "Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)", which is what Javascript code ""+(new Date()) gives):

$time = strtotime($dateWithTimeZone);
$dateInLocal = date("Y-m-d H:i:s", $time);

Or if you don't, which is likely from MySQL, then:

$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);

Alternatively

date() and localtime() both use the local timezone for the server unless overridden; you can override the timezone used with date_default_timezone_set() .

you may find these links useful

Set Default timezone

date

local-time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM