【问题标题】:Laravel / Livewire - Date is off by one dayLaravel / Livewire - 日期提前一天
【发布时间】:2021-05-06 18:19:24
【问题描述】:

我不确定这是从哪里来的,但默认的 HTML 日期选择器在使用 laravellivewire 时关闭了一天。

这段代码:

<div class="col-span-6 sm:col-span-4">
    <x-jet-label for="birthdate" value="{{ __('Geburtsdatum') }}" />
    <x-jet-input id="birthdate" name="birthdate" type="date" class="mt-1 mb-6 w-full" wire:model="details.birthdate"/>
    <x-jet-input-error for="details.birthdate" class="mt-2" />

    {{$details->birthdate}}
</div>

结局是这样的:

我使用此代码来确保我的模型中的生日格式符合我的预期:

protected $casts = [
    'birthdate' => 'date:Y-m-d',
];

与我的 livewire 组件中的验证规则相同:

protected $rules = [
    'details.birthdate' => 'date:Y-m-d',
];

我还在我的mysql 数据库中使用date 格式。

并且该值似乎与2021-02-02 一样正确,但使用wire:model 插入它最终为2021-02-01

有人知道这种行为的来源吗?

【问题讨论】:

  • 时区问题,也许? 2021-02-02 午夜 CET 仍然是 2021-02-01 UTC ..
  • 感谢您的快速回复@Mark Reed。我的 config/app.php 文件中的 timezone-attribute 设置为“Europe/Berlin”。因为我来自德国,所以这应该是正确的。还是我缺少其他配置设置?
  • MySQL 服务器的时区是什么?
  • 这是一个来自aws的eu-central-1实例。

标签: php laravel date datetime laravel-livewire


【解决方案1】:

最后我们找到了导致问题的原因。我们的model 中有一个受保护的变量:

protected $dates = ['birthdate'];

删除后,如果终于工作了。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2014-02-22
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2012-01-21
    • 2021-03-22
    • 1970-01-01
    • 2016-08-22
    • 2021-06-12
    相关资源
    最近更新 更多