1

I am using the date format in my php controller like

$meter->created_at = date('Y-m-d H:i:s');

But the output I am getting is 2017-11-29 00:00:00

I have also checked this solution.

Update 1

I am inserting some data via excel file.

foreach($final_data as $key=>$value)
    {
        if($key <= $header_index) continue;
        $meter = new Meters;

        foreach($value as $k=>$v){
            $v = preg_replace('/\s+/', ' ', trim($v));
            if(isset($fieldSet[0]['meter_msn']) && $fieldSet[0]['meter_msn']==$k){
                $meter->meter_msn =$v."";
                // echo $v.", ";
            }

            if(isset($fieldSet[0]['description']) && $fieldSet[0]['description']==$k){
                $meter->description = $v;
            }

            if (isset($fieldSet[0]['status']) && $fieldSet[0]['status'] == $k) {
                $meter->status = $v;
            }

            if (isset($fieldSet[0]['meter_status']) && $fieldSet[0]['meter_status'] == $k) {
                $meter->meter_status = $v;
            }

            if (isset($fieldSet[0]['historic']) && $fieldSet[0]['historic'] == $k) {
                $meter->historic = $v;
            }

        }
        $meter->created_at = date('Y-m-d H:i:s');
        if($meter->save())
            $ok_count++;
        else
            $status_arr[] = $meter->errors;

    } 

Any help would be highly appreciated.

Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

1

Keep the datatype for date fields like created_at and update_at as int and use TimestampBehaviour in your model.

By default, TimestampBehavior will fill the created_at and updated_at attributes with the current timestamp when the associated AR object is being inserted. it will fill the updated_at attribute with the timestamp when the AR object is being updated. The timestamp value is obtained by time() and whenever and where ever you want to show the date you can format it using php's date() function no need to make it complex when the framework itself can do it via conventional approach.

You might have to make little change for renaming the columns to created_at and updated_at and then include the TimestampBehaviour like below in your model.

use yii\behaviors\TimestampBehavior;

 public function behaviors() {
        return [
            TimestampBehavior::className(),
        ];
    }

Or if you want to use the column names as is you can use the following approach to map you field names

use yii\db\Expression;

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'create_time',
            'updatedAtAttribute' => 'update_time',
            'value' => new Expression('NOW()'),
        ],
    ];
}

TimestampBehavior also provides a method named touch() that allows you to assign the current timestamp to the specified attribute(s) and save them to the database. For example,

$model->touch('creation_time');
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68