PHP 偏移 unix 时间戳

标签 php datetime timezone unix-timestamp

我在 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/

相关文章:

php - 你能看到 PHP 脚本客户端吗?

php - 如何使用 jquery.serialize() 序列化 ul 中的所有 li#id?

mysql - DATETIME VS INT 用于存储时间?

java - input.equals ("string here")不工作

php - 寻找对相似数据进行分组的算法

php - preg_match() : Compilation failed: character value in\x{} or\o{} is too large at offset 27 on line number 25

c# - 如何将整数日期转换为格式化日期字符串(即 2012009 到 2/01/2009)

c++ - 快速转换 std::chrono::time_point 到/从 std::string

javascript - 如何将 JavaScript 日期与特定时区的日期进行比较?

javascript - 将 UTC 时间转换为十进制时间