PHP偏移unix时间戳 [英] PHP Offsetting unix timestamp

查看:149
本文介绍了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时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆