Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Offsetting unix 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.

like image 497
Justin Avatar asked Mar 10 '26 02:03

Justin


1 Answers

You should pass the original timestamp as the second parameter to strtotime.

$new_timestamp = strtotime("-7 hours", $timestamp);
like image 145
xdazz Avatar answered Mar 11 '26 15:03

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!