Hi is possible automatically 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!
Hi is possible automatically 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!
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
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.