Search code examples
phputcpst

How to convert UTC datetime into PST datetime in php


$utc_date = '2020-07-31T00:00:00.000Z';

Now i want this date in yyyy-mm-dd hh:mm:ss format like (2020-07-31 00:00:00), So can we achieve in PHP? How can we do it in easiest way?


Solution

  • Like this.

    $utc_date = '2020-07-31T00:00:00.000Z';
    $jsDateTS = strtotime($utc_date);
    if ($jsDateTS !== false) 
        echo date('Y-m-d H:i:s', $jsDateTS );
    

    Edit: Changed code to include timezone change.

    $utc_date = '2020-07-31T00:00:00.000Z';
    $timestamp = strtotime($utc_date);
    $date = new DateTime();
    $date->setTimestamp($timestamp);
    $date->setTimezone(new \DateTimeZone('America/Los_Angeles'));
    echo $date->format('Y-m-d H:i:s') . "\n";
    

    Working Example.