In Laravel 6, without changing the frame default timezone: Asia/Shanghai, the datetime format is adjusted to UTC (2)
1. Since the time zone of the program body is: Asia/Shanghai, but in some models, the time zone value of UTC is planned to be stored
2. Reference:https://www.shuijingwanwq.com/2022/07/11/6776/, the findings are still not as expected. After adjustment in step 7. The same program, after running in the queue, the value of the field updated_at is sometimes still: Asia/Shanghai, and most of the time zone is: UTC. as shown in Figure 1
3. Temporarily adjusted to the following implementation, when $value is a string, the current UTC time is returned.
/**
* 转换时区
*
* @param $value
* @return string
*/
private function transTz($value = null): string
{
return now()->utc()->toDateTimeString();
}
4. It is found that the time zone is still: Asia/Shanghai. However, it is found that the time zone is constantly changing, which may be caused by the constant save() in the queue. The save() method is executed more than 5 times in the same model in the queue. as shown in Figure 2
5. The final decision to adopt the plan that follows the original step 4. However, the scheme of step 4 does not apply to the delete() method, and the time zone of the value of deleted_at is still assia/shanghai in the SQL generated by it. as shown in Figure 3
update `table` set `deleted_at` = '2023-01-12 11:19:17' where `id` = 534
6. Decide to still use the modifier, because it is in the same queue, the same model will only execute the delete() method once.
/**
* 设置删除时间
*
* @param $value
* @return void
*/
public function setDeletedAtAttribute($value)
{
$this->attributes['deleted_at'] = $value ? now()->utc()->toDateTimeString() : $value;
}
7. The final SQL found that the generated SQL does not meet the expectations, and the time zone is still: Asia/Shanghai
update `table` set `deleted_at` = '2023-01-12 16:05:00' where `id` = 531
8. Try to overwrite the method: runsoftdelete() because soft delete uses trait softdeletes
Before editing:
$time = $this->freshTimestamp();
After editing:
$time = Date::now('UTC');
9. It is found that the generated sql meets the expectations, the time zone is: UTC
update `table` set `deleted_at` = '2023-01-12 08:09:02' where `id` = 534
10. In the model that needs to use the UTC timezone, the final implementation is as follows
use Illuminate\Database\Eloquent\SoftDeletes;
class ThemeInstallation extends Model
{
use SoftDeletes;
/**
* Perform the actual delete query on this model instance.
*
* @return void
*/
protected function runSoftDelete()
{
$query = $this->setKeysForSaveQuery($this->newModelQuery());
$time = Date::now('UTC');
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
file_put_contents(storage_path() . '/logs/SoftDeletes.runSoftDelete-' . microtime(true) . '-' . mt_rand() . '.txt', print_r($time, true), FILE_APPEND | LOCK_EX);
$this->{$this->getDeletedAtColumn()} = $time;
if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$query->update($columns);
$this->syncOriginalAttributes(array_keys($columns));
}
}
11. Since there are multiple models that need to use UTC timezone, in order to avoid code redundancy, create a new trait file: /app/traits/utcsoftdeletes.php
setKeysForSaveQuery($this->newModelQuery());
$time = Date::now('UTC');
$columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
$this->{$this->getDeletedAtColumn()} = $time;
if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$query->update($columns);
$this->syncOriginalAttributes(array_keys($columns));
}
}
12. In the model that needs to use UTC, introduce a new trait file to see the difference between the unused new trait and the new trait used. 8 hours apart. as shown in Figure 4



