It seems strange to me that two systems that have been around for so long and worked so closely together, do not manipulate dates in the same way.
It is also so easy to waste hours of time trying to manipulate dates, check that date A is less than date B and so on…
Using Events in a Yii model
The Yii model, or more accurately the CActiveRecord class, provides a number of hooks with which you can customise your workflow on database reads and updates.
Using the afterFind and beforeSave events enables you to intercept and modify data before passing it on either to User/view layer or back to the database.
PHP date class
On this basis, you can modify date formats between the two layers as follows:
| PHP | | copy code | | ? |
| 01 | public function beforeSave() |
| 02 | { |
| 03 | //PHP dates are displayed as dd/mm/yyyy |
| 04 | //MYSQL dates are stored as yyyy-mm-dd |
| 05 | $from=DateTime::createFromFormat('d/m/Y',$this->booking_from); |
| 06 | $this->booking_from=$from->format('Y-m-d'); |
| 07 | |
| 08 | $to=DateTime::createFromFormat('d/m/Y',$this->booking_to); |
| 09 | $this->booking_to=$to->format('Y-m-d'); |
| 10 | |
| 11 | parent::beforeSave(); |
| 12 | return true; |
| 13 | } |
| 14 | |
| 15 | public function afterFind() |
| 16 | { |
| 17 | //PHP dates are displayed as dd/mm/yyyy |
| 18 | //MYSQL dates are stored as yyyy-mm-dd |
| 19 | $from=DateTime::createFromFormat('Y-m-d',$this->booking_from); |
| 20 | $this->booking_from=$from->format('d/m/Y'); |
| 21 | |
| 22 | $to=DateTime::createFromFormat('Y-m-d',$this->booking_to); |
| 23 | $this->booking_to=$to->format('d/m/Y'); |
| 24 | |
| 25 | parent::afterFind(); |
| 26 | return true; |
| 27 | } |
You could, of course, add locale dependant formatting if required.
It is worth noting that;
i) afterSave will leave your date in MySql format. You will either need to force a read or convert back to PHP format with an afterSave event
ii) Your events must return true otherwise this can cause you problems such as the model->save() not working for no apparent reason.
I have wondered whether to set the model->dateField to the PHP date class and then in any views to convert using $date->format. This would also make it much simple to do date calculations in the controller or model.
Wouldn’t it be nice if all this were handle within Yii….
Blog
Photography
Yii Framework
My Portfolio
Can I simply just say what a relief to discover somebody who genuinely understands what they’re discussing on the web. You actually realize how to bring an issue to light and make it important. A lot more people really need to look at this and understand this side of your story. It’s surprising you are not more popular given that you certainly possess the
gift.
Hi Jerilyn
Thank you for your kind comments.
Chris
Hey there! I know this is kind of off topic but I was wondering which blog
platform are you using for this website? I’m getting tired of WordPress because I’ve had
problems with hackers and I’m looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.
Hello
This is running on WordPress but I will shortly be converting to a Yii based blogging, eCommerce & site management system that I am developing – which will be open source…