Jump to content


Photo

Yii2 - How to insert a date with the correct timestamp?


  • Please log in to reply
No replies to this topic

#1 benoit1980

benoit1980

    Member

  • Members
  • PipPip
  • 26 posts

Posted 11 May 2014 - 11:35 AM

Hi All,

 

I would like to know if someone could help me with this, I have been struggling for days and cannot find an answer.

I am new to OOP and still struggle a bit with the logic.

 

I have installed Yii2 on my local machine, all I want is insert a date in the correct format in the database as:

DD/MM/YYYY

 

I cannot believe how hard it is just ti work this one out as a beginner.......

 

I tried this:

http://www.yiiframew...behaviors.html:

 

And added this code inmy Posts Model:

 

 

use yii\behaviors\TimestampBehavior;

class 
User extends ActiveRecord
{
    
// ...

    
public function behaviors()
    {
        return [
            
'timestamp' => [
                
'class' => TimestampBehavior::className(),
                
'attributes' => [
                    
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at''updated_at'],
                    
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
            ],
        ];
    }
}

 

 

 

I have the corresponding database fields as created_at and updated_at set as INT.

 

 

Unfortunately, when I try to save my form I get this error:

PHP Fatal Error – yii\base\ErrorException
 
Class 'app\models\ActiveRecord' not found
1. in C:\wamp\www\yii2\models\posts.php at line 61
    }
 
public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ],
            ],
        ];
    }
 
 
 
}

 

 

 

Any idea why please?I have followed the Yii website tutorial correctly but still cannot get the date input to work.

 

 

Thank you,

 

 

Regards,

 

Ben

 


Edited by benoit1980, 11 May 2014 - 11:37 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com