-1
$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?

pika-pika
  • 57
  • 1
  • 6
  • 1
    Does this answer your question? [How can I easily convert dates from UTC via PHP?](https://stackoverflow.com/questions/952975/how-can-i-easily-convert-dates-from-utc-via-php) – Always Helping Jul 31 '20 at 09:53

1 Answers1

1

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.

Umair Khan
  • 1,566
  • 16
  • 32
  • PST is 7 hrs behind to UTC. I am not talking about only format, So i should get following output **2020-07-30 04:00:00** for above date – pika-pika Jul 31 '20 at 10:16
  • Sorry for the typo – pika-pika Jul 31 '20 at 10:29
  • may i know why you added `new \DateTimeZone('America/Los_Angeles')` – pika-pika Jul 31 '20 at 10:30
  • 1
    The php play ground I am using has default timezone set `UTC`. So I added that line to set different timezone for formatted date time so that it can be converted from to `UTC` to `America/Los_Angeles` as it has 7 hours difference from UTC. Also see [this](https://stackoverflow.com/a/3905222/4903314). – Umair Khan Jul 31 '20 at 10:35
  • 1
    Thank you! This was really helpful and got the optimized solution. – pika-pika Jul 31 '20 at 10:43