Convert UTC dates to local time in PHP

前端 未结 10 1493
青春惊慌失措
青春惊慌失措 2020-11-28 07:16

I\'m storing the UTC dates into the DB using:

$utc = gmdate(\"M d Y h:i:s A\");

and then I want to convert the saved UTC date to the client

相关标签:
10条回答
  • 2020-11-28 07:26

    First, get the date in UTC -- you've already done that so this step would really just be a database call:

    $timezone = "UTC";
    date_default_timezone_set($timezone);
    
    $utc = gmdate("M d Y h:i:s A");
    print "UTC: " . date('r', strtotime($utc)) . "\n";
    

    Next, set your local time zone in PHP:

    $timezone = "America/Guayaquil";
    date_default_timezone_set($timezone);
    

    And now get the offset in seconds:

    $offset = date('Z', strtotime($utc));
    print "offset: $offset \n";
    

    Finally, add the offset to the integer timestamp of your original datetime:

    print "LOCAL: " . date('r', strtotime($utc) + $offset) . "\n";
    
    0 讨论(0)
  • 2020-11-28 07:28

    Here is a straight way to convert the UTC time of the questioner to local time. This is for a stored time in a database etc., i.e. any time. You just have to find the time difference between UTC time and the local time you are interested in and then ajust the stored UTC time adding to it the difference.

    $df = "G:i:s";  // Use a simple time format to find the difference
    $ts1 = strtotime(date($df));   // Timestamp of current local time
    $ts2 = strtotime(gmdate($df)); // Timestamp of current UTC time
    $ts3 = $ts1-$ts2;              // Their difference
    

    You can then add this difference to the stored UTC time. (In my place, Athens, the difference is exactly 5:00:00)

    Example:

    $time = time() // Or any other timestamp
    $time += $ts3  // Add the difference
    $dateInLocal = date("Y-m-d H:i:s", $time);
    
    0 讨论(0)
  • 2020-11-28 07:30

    PHP's strtotime function will interpret timezone codes, like UTC. If you get the date from the database/client without the timezone code, but know it's UTC, then you can append it.

    Assuming you get the date with timestamp code (like "Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)", which is what Javascript code ""+(new Date()) gives):

    $time = strtotime($dateWithTimeZone);
    $dateInLocal = date("Y-m-d H:i:s", $time);
    

    Or if you don't, which is likely from MySQL, then:

    $time = strtotime($dateInUTC.' UTC');
    $dateInLocal = date("Y-m-d H:i:s", $time);
    
    0 讨论(0)
  • 2020-11-28 07:31

    I store date in the DB in UTC format but then I show them to the final user in their local timezone

    // retrieve
    $d = (new \DateTime($val . ' UTC'))->format('U');
    return date("Y-m-d H:i:s", $d);
    
    0 讨论(0)
  • 2020-11-28 07:33

    Answer

    Convert the UTC datetime to America/Denver

    // create a $dt object with the UTC timezone
    $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('UTC'));
    
    // change the timezone of the object without changing it's time
    $dt->setTimezone(new DateTimeZone('America/Denver'));
    
    // format the datetime
    $dt->format('Y-m-d H:i:s T');
    

    Notes

    time() returns the unix timestamp, which is a number, it has no timezone.

    date('Y-m-d H:i:s T') returns the date in the current locale timezone.

    gmdate('Y-m-d H:i:s T') returns the date in UTC

    date_default_timezone_set() changes the current locale timezone

    to change a time in a timezone

    // create a $dt object with the America/Denver timezone
    $dt = new DateTime('2016-12-12 12:12:12', new DateTimeZone('America/Denver'));
    
    // change the timezone of the object without changing it's time
    $dt->setTimezone(new DateTimeZone('UTC'));
    
    // format the datetime
    $dt->format('Y-m-d H:i:s T');
    

    here you can see all the available timezones

    https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

    here are all the formatting options

    http://php.net/manual/en/function.date.php

    Update PHP timezone DB (in linux)

    sudo pecl install timezonedb
    
    0 讨论(0)
  • 2020-11-28 07:44

    date() and localtime() both use the local timezone for the server unless overridden; you can override the timezone used with date_default_timezone_set().

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

    http://us3.php.net/manual/en/function.date.php

    http://php.net/manual/en/function.localtime.php

    0 讨论(0)
提交回复
热议问题