Mohammad Sulaiman
Mohammad Sulaiman

Reputation: 21

How to format date from database in Yii2

If I have function like this

protected function getAreaValues($model)
	{
		return 
		[
			[
				$model->getAttributeLabel('EXPDATE'),TbArea::findOne($model->KODE)->EXPDATE
			],	

		];
	}

where EXPDATE IS date data

How to format it in d-M-Y ?

I add formatter in web.php in config like this

		'formatter' => [
			'class' => 'yii\i18n\Formatter',
			'nullDisplay' => '-',
			'dateFormat' => 'd-M-Y',
			'datetimeFormat' => 'd-M-Y H:i:s',
			'timeFormat' => 'H:i:s',			
		],

But still not working

Upvotes: 1

Views: 13778

Answers (2)

Red Bottle
Red Bottle

Reputation: 3080

I think this will work

$DateTime = DateTime::createFromFormat('Y-m-d', $yourOldDateString);
$newDateString = $DateTime->format('d/M/Y');

Upvotes: 0

Irfan Ali
Irfan Ali

Reputation: 2258

You can format date like below.

echo Yii::$app->formatter->asDate('2017-03-30', 'd-M-Y'); // 30-Mar-2017

try this.

and you can check Yii2 doc

Yii2 Formatters

Upvotes: 1

Related Questions