gpt4 book ai didi

PHP 偏移 unix 时间戳

转载 作者:行者123 更新时间:2023-12-02 07:40:01 28 4
gpt4 key购买 nike

我在 PHP 中有一个 unix 时间戳:

$timestamp = 1346300336;

然后我有一个我想要应用的时区的偏移量。基本上,我想应用偏移量并返回一个新的 unix 时间戳。偏移量遵循这种格式,遗憾的是由于与其他代码的兼容性无法更改:

$offset_example_1 = "-07:00";
$offset_example_2 = "+00:00";
$offset_example_3 = "+07:00";

我试过了:

$new_timestamp = strtotime($timestamp . " " . $offset_example_1);

但不幸的是,这不起作用 :(。还有其他想法吗?

编辑

经过测试,我 super 惊讶,但即使这样也行不通:

strtotime("1346300336 -7 hours")

返回 false。

让我们稍微改变一下,将上面的偏移示例转换为秒的最佳方法是什么?然后我可以简单地做 $timestamp + $timezone_offset_seconds

最佳答案

您应该将原始时间戳作为第二个参数传递给 strtotime

$new_timestamp = strtotime("-7 hours", $timestamp);

关于PHP 偏移 unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12190510/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com