How to display date in correct format?

Hi,

I used the yiic to generate crud but it don't display the date fields in local format.

My model have a datetime field in mysql default format "YYYY-MM-DD HH:MM:SS"

But I want show in this format "DD/MM/YYYY HH:MM"(seconds without)

In the form I used a CMaskedTextField to show at the desired format, but i had a lot of problems to 'unmask' it.

Can anyone help me?

Sorry,

this should be in yii 1.0.

:-X

If I understood you correctly, you want to convert ‘DD/MM/YYYY HH:MM’ to ‘YYYY-MM-DD HH:MM:SS’.

If so, here’s a snippet:


if(preg_match('#(d{2})/(d{2})/(d{4}) (d{2}):(d{2})#', $time, $m))

	$time = date("Y-m-d H:i:s", mktime($m[4], $m[5], 0, $m[2], $m[1], $m[3]));



I rather suggest you to use unix time integers when customized time format is required.

thanks guy.

but I wanna more than this.

I would know how can I automate this. When view was called the date format is ‘DD/MM/YYYY HH:MM’ and when I persist the data this date values be in ‘YYYY-MM-DD HH:MM:SS’ (or in unix time, as you said.)

Are you looking for strtotime() ?

yes.

I’m using strtotime().

But I think that I find what I need. Anything like getters/setters. Now I’m learning how to use getters/setters in Yii.