Date Time function producing incorrect result in PHP

Viewed 38

I have this function in PHP:

function dt_utc_to_local($dt_utc, $timezone)

{

$dt_utc2 = new DateTimeImmutable($dt_utc, new DateTimeZone('UTC'));
$dt_local_time = $dt_utc2->setTimezone(new DateTimeZone($timezone)); 
$dt_local_time_formated = $dt_local_time->format('Y-m-d H:i:s');

$dt_now_utc = new DateTimeImmutable(gmdate("Y-m-d H:i:s"), new DateTimeZone('UTC'));
$dt_now_local_time = $dt_now_utc->setTimezone(new DateTimeZone($timezone));
$now_dt_local_time_formated = $dt_now_local_time->format('Y-m-d H:i:s');

return array($now_dt_local_time_formated, $dt_local_time_formated );

}

That I call using:

$msg_date = '2022-09-14 12:00:00';
$timezone = 'America/New_York';

list($dt_now_local_time, $dt_local_time ) = dt_utc_to_local($msg_date, $timezone);

This is to convert UTC datetime that I have in a MySql database to local datetime, but for some reason $dt_now_local_time keeps giving me the date of September 13 and today is 14.

If I use the same code without a function, it works well.

Any idea?

0 Answers
Related