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
CodePudding user response:
<?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";
?>
CodePudding user response:
<?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";