PHP偏移unix时间戳

编程入门 行业动态 更新时间:2024-10-09 16:23:22
本文介绍了PHP偏移unix时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 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时间戳

本文发布于:2023-05-25 19:52:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/236214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!