Search code examples
phpdatetimeunix-timestamptimezone-offset

Add time offset from GMT to Unix timestamp PHP


I'd like to convert Unix timestamp from GMT to server time. How to convert the time offset to seconds in order to add it to the timestamp? Or is there any better way?

$t = 1470565421;
$off = (new DateTime('now', new DateTimeZone(date_default_timezone_get())))->format('P'); // returns +02:00
$t2 = $t + convertToSec($off);

Solution

  • Depending on the user, set the default timezone using

    http://php.net/manual/en/function.date-default-timezone-set.php

    date_default_timezone_set('America/Los_Angeles');
    

    That will take away your need to add/substract GMT to/from timestamps etc whithin functions!