When I create a user I’m saving the date on which he was created with this code in beforeValidate function:
$this->create_time=new CDbExpression('NOW()');
Now , in addition , I need to determine on what season he was created , which means I need to get the month from the create_time field , which is date , as integer.
I tried several php codes but none worked , and now found that yii has some formatMonth() method , but couldn’t find a clear example how to do this.
because $this->create_ses is a formatted datetime, php date function expect parameter 2 to be a timestamp that’s why you need strtotime function to convert it to timestamp.
doesn’t execute any SQL query to evaluate the expression. CDbExpression is just a wrapper object around ‘NOW()’ string that indicates that it’s an SQL expression instead of an explicitly defined value. The actual value of create_time is unknown until the record gets saved. The eaiest solution is probably to create timestamp at PHP level:
$this->create_time=date('Y-m-d H:i:s');
Also, storing both create_time and create_ses in the database seems to be redundant and likely to cause maintenance issues. Create_ses can be calculated runtime when required.