0

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();

}
3
  • So couple of questions. 1. Are you using $useTimestamps in your model? 2. You can use the initialize() function which allows you to run a query for the model without having to modify the base model. 3. Bear in mind a utc timestamp will be just that. You might want to set php.net/manual/en/function.date-default-timezone-set.php and see if that fixes it. Commented Oct 29, 2024 at 13:12
  • Thank you for pointing. I did not change useTimestamps from false to true. This worked. Commented Nov 1, 2024 at 4:11
  • @Antony please create an answer with $useTimestamps=true is the solution so I could mark it correct answer Commented Nov 1, 2024 at 4:12

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.