Thursday, February 28, 2013

How easly format dates in Yii

Very useful tools in Yii are CDateTimeParser and CDateFormatter
Let's assume we have date and time formatted like this: "28/02/2018 14:10"
It might be output from MySql for example.

How to convert it to "28 feb 2018, 14:10" without any hassle?
<?php
$unixtime=CDateTimeParser::parse($model->time,'dd/MM/yyyy hh:mm');
echo Yii::app()->dateFormatter->format('dd MMM yyyy, hh:mm',$unixtime); 
?>
More:
CDateFormatter
CDateTimeParser

1 comment:

  1. Newbie question:
    Why can't we use CDateFormatter::format() just like CDateTimeParser::parse()?

    ReplyDelete