Get Localized Date/Time

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?

I finally managed to get functional code. Should anyone have the same question, my solution was

$date = new DateTime(Yii::$app->formatter->asDateTime('now', 'yyyy/MM/dd HH:mm:ss'));

The advantage here is the Locale is only defined in the config file, instead of specifying it at every use.

I still haven’t found a way to simply do

$date = new DateTime(Yii::$app->formatter->asDateTime('now'));

so I don’t need to specify a format, and use the default format specified in the config?!

Correction, it does work, it appears my formatter values were incorrect

so doing something like the following works perfectly.
Yii::$app->formatter->asDate('now')
Yii::$app->formatter->asTime('now')
Yii::$app->formatter->asDateTime('now')