【问题标题】:Converting MySQL Timestamp with GMT offset to local PHP timestamp将具有 GMT 偏移量的 MySQL 时间戳转换为本地 PHP 时间戳
【发布时间】:2012-07-15 12:46:26
【问题描述】:

我有一个外部供应商调用的 PHP 脚本。 GET 值之一是 MySQL 格式的时间戳,GMT + 0200。这是一个示例:

2012-07-16 16:51:22

我可以将它转换成我使用后的格式:

$converted =  date ("m/d/Y h:i:s A", strtotime ($ts) ); 

但是我也想将其转换为当地时间,例如GMT + 10。我对最后一点感到困惑 - 如果有人能推荐一些在时区/GMT 偏移之间转换的选项,我将不胜感激,因为我不确定要使用哪个 PHP 函数。

【问题讨论】:

标签: php timestamp timestamp-with-timezone


【解决方案1】:

使用 DateTime,像这样:

$date = new DateTime($ts);
$date->setTimeZone(new DateTimeZone("Europe/Amsterdam");
$converted = date_format($date, 'm/d/Y h:i:s A');

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    相关资源
    最近更新 更多