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
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 vendorcodeigniter4frameworksystemModel.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();
}
2
Answers
If your whole project is using IST timezone, you can set the default timezone in your
index.php
file.You can use
PHP
date_default_timezone_set.Just add this line of code into your
index.php
file after the<?php
tag.To check, just add
And also you can refer to
PHP
List of Supported Timezones to check what timezone you will going to use.Hope this helps!
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