简体   繁体   English

在UTC中保存日期时,我可以在本地时区显示日期吗?

[英]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 您可能会发现这些链接有用

Set Default timezone 设置默认时区

date 日期

local-time 当地时间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将本地日期时间转换为UTC日期时间 - Convert local date time to UTC date time “date_part('epoch',now()在时区'UTC')”与postgresql中时区'UTC'的“now()不同时” - “date_part('epoch', now() at time zone 'UTC')” not the same time as “now() at time zone 'UTC'” in postgresql 在PHP中将本地日期时间转换为UTC时间 - convert local date time to UTC time in php PHP-date('now')的输出比本地时间早6小时,但是时区设置为正确的本地时区吗? - PHP - output of date('now') is a ahead of local time by 6 hours, but time zone is set to correct local zone zone? 根据访问者的本地时区将数据库中保存的 UTC 时间显示为访问者的本地时间 - show saved utc time in database as invisitor's local lime based on his local time zone 将日期/时间从本地服务器时间转换为时区? - convert date/time from local server time to time zone? 如何在PHP中从本地时间(无日期)获取UTC时间? - How to get UTC time from local time (without date) in PHP? 日期和时区偏移 - Date and Time Zone offset PHP日期函数返回错误的日期,如何指定我的时区? - PHP date function returning wrong date, how can I specify my time zone? 更改简单的日期时间格式以显示时区 - Change the simple date time format to show time zone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM