【问题标题】:Carbon: return a timestamp based on input date string AND timezoneCarbon:根据输入日期字符串和时区返回时间戳
【发布时间】:2016-10-06 13:09:57
【问题描述】:

以欧洲/巴黎 (+02:00) 的某人为例。

我想构建一个函数,在其中传入日期和时间字符串('2016-10-6 06:00 PM')和我的时区标识符('Europe/Paris')。我想返回对应的 GMT 时间戳。

getTimestamp('2016-10-6 06:00 PM', 'Europe/Paris')

这是我目前所拥有的:

public function getTimestamp($string, $time_zone)
{
    $c = Carbon::parse($string);
    $c->timezone = new DateTimeZone($time_zone);
    return $c->timestamp;
}

问题

如果传递了2016-10-06 06:00 PM 的日期和时间以及Europe/Paris 的时区,我将返回对应于格林威治标准时间2016-10-06 06:00 PM 的UTC 时间戳。

它应该返回格林威治标准时间 04:00 的时间,因为欧洲/巴黎提前两个小时。

问题

我如何传递日期和时区并取回具有正确偏移量的 GMT 时间戳。

【问题讨论】:

    标签: php date datetime php-carbon


    【解决方案1】:

    解析函数可以让你传入时区

    public static function parse($time = null, $tz = null)
    

    所以把它传入:

    $c = Carbon::parse($string, $time_zone);
    

    【讨论】:

    • 我绝对没有在文档中看到这一点。它工作得很好,这将为我节省很多压力!谢谢!
    • 我也没有在文档中看到,但是如果有疑问,请查看源代码。欢迎!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2012-10-25
    • 1970-01-01
    • 2014-03-07
    • 2022-01-03
    • 2014-10-22
    • 1970-01-01
    相关资源
    最近更新 更多