没有不值得去解决的问题,也没有不值得去学习的技术!

In Laravel 6, the datetime format is adjusted to UTC without changing the frame default timezone: Asia/Shanghai

插入表中的数据为 UTC 时间,符合预期

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

最终生成的值与东八区一致,因为当前时区设置为东八区。打印:config('app.timezone') ,其值为:"Asia/Shanghai"
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

之前的实现方案二,数据库迁移文件无变化,但是模型文件中,$timestamps 属性设置为 false。然后在插入记录时,直接定义为:now()->utc()->timestamp。now 函数为当前时间创建一个新的 Illuminate\Support\Carbon 实例。其值相对于东八区时间,提前了 8 小时。
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

插入表中的数据为 UTC 时间,符合预期
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

但是,当使用 push() 方法时,日期时间仍然为:"Asia/Shanghai"。总记 3 条 SQL,前 2 条 SQL 使用 UTC ,第 3 条 SQL 使用的 Asia/Shanghai
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


需要长期技术维护或远程问题排查?

我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。

如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:

  • ✅ PHP / Laravel / Yii2 老项目无人维护
  • ✅ Go / Gin 后端接口需要排查或优化
  • ✅ WordPress 网站访问慢、报错或插件冲突
  • ✅ Nginx / MySQL / Redis / Linux 服务器异常
  • ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
  • ✅ 需要长期远程技术支持或兼职维护

更多介绍请查看:关于我 & 合作

微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理