phpmysqltimezonecodeigniter-4

Setting mysql timezone same as php


I am trying to set the timezone for codeigniter v4 and if I set using php code, it is setting in IST, whereas the updated_at column is still at UTC timezone.

From the below records

enter image description here

the created_at and last_active_on, both columns, are in IST timezone.

The updated_at is in UTC timezone. How do I set the timezone for the mysql specific only for this database.

I tried from link with below code

    $this->db->query("SET time_zone='+05:30'");

in vendor\codeigniter4\framework\system\Model.php

it did not work. Also this is changing the library file, which is not recommended.

Below is my code (incase if you need a reference)

$activityObj = new Activity;

$activity = $activityObj->where('user_id', $user['id'])->first();


$data = ['last_active_on' => date('Y-m-d H:i:s')];

if (empty($activity)) {

    $data['user_id'] = $user['id'];

    $data['created_at'] = date('Y-m-d H:i:s');

    $activityObj->save($data);

} else {

    $activityObj->where('user_id', $user['id'])
        ->set($data)
        ->update();

}

Solution

  • The answer appears to be to change the $useTimestamps parameter which is part of the default Model as found here: https://codeigniter4.github.io/CodeIgniter4/models/model.html#usetimestamps