I have been struggling with Yii to get localized date time value to display. So I figured I’d go back to basics.
I was trying
$date = new DateTime(date('Y-m-d H:i:s'));
but that was 6 hours off? So then I switched to
$date = new DateTime("now", new DateTimeZone('America/New_York'));
which works properly.
Now in Yii, I’ve set my config to include things like
'formatter' => [ 'class' => 'yii\i18n\Formatter', 'defaultTimeZone' => 'EST', 'timeZone' => 'America/New_York', 'dateFormat' => 'Y-m-d', 'datetimeFormat'=>'Y-m-d H:i:s', 'timeFormat'=>'H:i:s', ],
So I thought I could use some builtin function such as asDatetime() but haven’t been able to figure it out. Could someone offer any guidance on the matter. Is using traditional PHP the way to do this, or since Yii already has the locale/timezone it has a better solution to offer?