'PHP doesn't recognize PDT
I'm just trying to print the current time in PDT
<?php
$date1 = new DateTime("now", new DateTimeZone('PDT') );
echo $date1->format('Y-m-d H:i:s e P')."\n";
$date2 = new DateTime("now", new DateTimeZone('PST') );
echo $date2->format('Y-m-d H:i:s e P')."\n";
?>
and I get
2022-05-03 14:59:33 PDT -08:00
2022-05-03 14:59:33 PST -08:00
Why is this happening and how do I get the right time? This is PHP 7.4
Edit: I know America/Los_Angeles works, but that's not the point. Background: get the system time zone from a database and get the current time of that time zone. Unless there's a way to convert PDT to America/Los_Angeles?
Solution 1:[1]
<?php
$date1 = new DateTime("now", new DateTimeZone('America/Los_Angeles') );
echo $date1->format('Y-m-d H:i:s e P')."\n";
$date2 = new DateTime("now", new DateTimeZone('America/Los_Angeles') );
echo $date2->format('Y-m-d H:i:s e P')."\n";
?>
Solution 2:[2]
<?php
// first set YOUR timezone or the Server timezone
date_default_timezone_set('America/Phoenix');
// set current time
$date1 = new DateTime();
echo $date1->format('Y-m-d H:i:s e P') . "\n";
// set new time zone
$newTimezone = new DateTimeZone('PST');
// change current Datetime to new timezone
$date1->setTimezone($newTimezone)
// show time
echo $date1->format('Y-m-d H:i:s e P')."\n";
// set new time zone
$newTimezone = new DateTimeZone('EST');
// change current Datetime to new timezone
$date1->setTimezone($newTimezone)
// show time
echo $date1->format('Y-m-d H:i:s e P')."\n";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pran Joseph |
| Solution 2 | Guido Faecke |
