In Laravel 6, the datetime format is adjusted to UTC without changing the frame default timezone: Asia/Shanghai
1. The previous implementation scheme 1, in the database migration file. $table->timestamps(); is equivalent to empty created_at and updated_at timestamp.
Schema::create($tableName, function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
2. In the model file, $timestamps is not set, the default is true, and eloquent automatically manages these two columns: created_at and updated_at.
3. The final generated value is the same as that of the East Eighth District, because the current time zone is set to the East Eighth District. print: config(app.timezone), its value is: “Asia/Shanghai”. as shown in Figure 1
4. In the previous implementation scheme 2, the database migration file is unchanged, but in the model file, the $timestamps property is set to false. Then when inserting the record, it is directly defined as: now()->utc()->timestamp. The now function creates a new instance of Illuminate\Support\Carbon for the current time. Its value is 8 hours earlier than the time in the East Eighth District. as shown in Figure 2
5. The final decision is to define a modifier. When we try to set the created_at and updated_at attribute values on the model, the modifier will be called automatically. The modifier gets the value that the attribute has been set, allowing you to modify and set its value to the $attributes property inside the Eloquent model.
/**
* 设置创建时间
*
* @param $value
* @return void
*/
public function setCreatedAtAttribute($value)
{
$this->attributes['created_at'] = $this->transTz($value);
}
/**
* 设置更新时间
*
* @param $value
* @return void
*/
public function setUpdatedAtAttribute($value)
{
$this->attributes['updated_at'] = $this->transTz($value);
}
/**
* 转换时区
*
* @param $value
* @return string
*/
private function transTz($value = null): string
{
return $value instanceof Carbon
? $value->utc()->toDateTimeString()
: ($value ?: now()->utc()->toDateTimeString());
}
6. The data inserted into the table is UTC time, which is in line with expectations. as shown in Figure 3
7. However, when using the push() method, the date time is still: “Asia/Shanghai”. A total of 3 pieces of SQL, the first 2 sqls use UTC, and the 3th SQL is used by ASIA/Shanghai. as shown in Figure 4
$this->themeInstallationTask->themeInstallation->processing = ThemeInstallation::PROCESSING_FALSE;
$this->themeInstallationTask->themeInstallation->processing_failed = ThemeInstallation::PROCESSING_FAILED_FALSE;
$this->themeInstallationTask->themeInstallationVersionPreset->processing = ThemeInstallationVersionPreset::PROCESSING_FALSE;
$this->themeInstallationTask->themeInstallationVersionPreset->processing_failed = ThemeInstallationVersionPreset::PROCESSING_FAILED_FALSE;
$this->themeInstallationTask->processing = ThemeInstallationTask::PROCESSING_FALSE;
$this->themeInstallationTask->processing_failed = ThemeInstallationTask::PROCESSING_FAILED_FALSE;
$this->themeInstallationTask->push();
update
`theme_installation_task`
set
`processing` = 0,
`theme_installation_task`.`updated_at` = '2022-06-21 10:02:50'
where
`id` = 18;
update
`theme_installation`
set
`processing` = 0,
`theme_installation`.`updated_at` = '2022-06-21 02:02:50'
where
`id` = 18;
update
`theme_installation_version_preset`
set
`processing` = 0,
`theme_installation_version_preset`.`updated_at` = '2022-06-21 02:02:50'
where
`id` = 18;
8. The reason should be that $value is not an instance of Carbon, so it returns $value as it is. You need to convert $value to an instance of Carbon .
/**
* 转换时区
*
* @param $value
* @return string
*/
private function transTz($value = null): string
{
if (is_null($value)) {
return now()->utc()->toDateTimeString();
}
return ($value instanceof Carbon) ? $value->utc()->toDateTimeString() : Carbon::parse($value, 'utc')->toDateTimeString();
}
9. When using the push() method, the datetime is already UTC. in line with expectations. as shown in Figure 5
10. However, when using the update() method, the datetime is still: “Asia/Shanghai”
ThemeInstallation::where('wp_theme_id', '<>', $id)->update(['role' => ThemeInstallation::ROLE_UNPUBLISHED]);
update
`theme_installation`
set
`role` = 'unpublished',
`theme_installation`.`updated_at` = '2022-06-21 13:38:36'
where
`wp_theme_id` <> 195
11. Decide to manually specify the updated_at during the update. in line with expectations.
ThemeInstallation::where('wp_theme_id', '<>', $id)->update(['role' => ThemeInstallation::ROLE_UNPUBLISHED, 'updated_at' => now()->utc()->toDateTimeString()]);
update
`theme_installation`
set
`role` = 'unpublished',
`theme_installation`.`updated_at` = '2022-06-21 07:39:46'
where
`wp_theme_id` <> 195



