我在 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);但是这样做的确不是工作不幸:(任何其他想法? / p>
编辑
测试后,我很惊讶,但即使这样也不工作:
strtotime(1346300336 -7小时)返回false。
让我们来看看有点不一样,上面的偏移示例的最好的方法是什么,到几秒钟,然后我可以简单地做 $ timestamp + $ timezone_offset_seconds 。
解决方案您应该将原始时间戳作为第二个参数传递给 strtotime 。
$ new_timestamp = strtotime( - 7小时,$ timestamp);
I have a unix timestamp in PHP:
$timestamp = 1346300336;Then I have an offset for timezones which I want to apply. Basically, I want to apply the offset and return a new unix timestamp. The offset follows this format, and unfortunately can't be changed due to compatibility with other code:
$offset_example_1 = "-07:00"; $offset_example_2 = "+00:00"; $offset_example_3 = "+07:00";I tried:
$new_timestamp = strtotime($timestamp . " " . $offset_example_1);But this does not work unfortunately :(. Any other ideas?
EDIT
After testing, I super surprised, but even this doesn't work:
strtotime("1346300336 -7 hours")Returns false.
Let's approach this a bit different, what is the best way to transform the offset examples above, into seconds? Then I can simply just do $timestamp + $timezone_offset_seconds.
解决方案You should pass the original timestamp as the second parameter to strtotime.
$new_timestamp = strtotime("-7 hours", $timestamp);
更多推荐
PHP偏移unix时间戳
发布评论