开发者

Yii custom date format (pattern)

Is there a way to specify your own date pattern besides the included ones (small, medium, full). The main point here is that it should work with i18n. I've tried a couple of things but I couldn't get it to work...

Yii::app()->dateFormatter->format("l d/m/Y",$slide->date_start);

I know about strftime but the problem her开发者_如何学编程e is that different hosting providers use different locale string... and you have to customize it...

I'm looking for an elegant way of doing this.

I'd like to display the date in l d/m/Y form...

Update: Never mind... I've just found out that dateFormatter doesn't use standard php date format...


I think you should measure time solely in Unix Time because Timezones & date formats are a presentation-layer problem. Unix time is always UTC & It's a single number, so easier to pass around in code.

As far the problem of "hosting providers use different locale string", just ask the user his timezone & display according to that. far less error-prone than trying to guess.

For date formatting, have a look at YII's format()

Hope it answers your question


Here's a related yii forum discussion

The yii forum solution worked for me to avoid raw SQL NOW() statements but still produce database-friendly date strings with PHP date() and time() functions which otherwise return integers.

In protected/config/main.php:

...
'params'=>array(
    'mysqlDateTimeFormat' => 'Y-m-d H:i:s', # ':u' adds microsecond precision,
...

Then, wherever you want to put a date-time string into a model field use

$myModel->myDate = date(Yii::app()->params['mysqlDateTimeFormat']);

Obviously you can enter the date/time format into the date (or time) functions directly if you prefer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜