开发者

Auto converting data from a datetime field to a specific format in CakePHP

开发者 https://www.devze.com 2022-12-13 10:56 出处:网络
Hi is possible automati开发者_开发技巧cally to format (using date()) all data from a datetime field in CakePHP?

Hi is possible automati开发者_开发技巧cally to format (using date()) all data from a datetime field in CakePHP? I'm thinking about using a callback function in the model but I don't know if I could filter fields coming from a datetime type.

Thanks in advance!


If it's something you just want to apply a single model you could use the afterFind and beforeSave callbacks to reformat the date.

If it's something you want to apply to many models you should create a Behaviour for it. You could use an array that tells it what date fields it should format.


I think I'm missing something in your question, but if you can get a date into a common string format, you can format it with PHP's date function and the strToTime function.

$date = 'January 27th, 2008'; //almost any format for dates in common usage
echo date('Y-m-d', strToTime($date));

//will print
2009-01-27


If you don't feel like hacking CakePHP's codebase, why don't you just code a wrapper function for find inside your controller? You will have to stick to a naming convention to easily identify your datetime fields but hey naming conventions are good right?

Something like: http://pastebin.com/mbbe91fe

0

精彩评论

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

关注公众号