[英]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. PHP的strtotime
函数将解释时区代码,例如UTC。 If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it. 如果您从数据库/客户端获取的日期没有时区代码,但知道它是UTC,则可以附加它。
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): 假设您获得带有时间戳记代码的日期(例如“ Fri Mar 23 2012 22:23:03 GMT-0700(PDT)”,这是Javascript代码""+(new Date())
给出的)):
$time = strtotime($dateWithTimeZone);
$dateInLocal = date("Y-m-d H:i:s", $time);
Or if you don't, which is likely from MySQL, then: 或者,如果不这样做(很可能来自MySQL),则:
$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; 除非被覆盖,否则date()
和localtime()
都使用服务器的本地时区。 you can override the timezone used with date_default_timezone_set()
. 您可以使用date_default_timezone_set()
覆盖时区。
you may find these links useful 您可能会发现这些链接有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.