gpt4 book ai didi

laravel - DateTime::__construct(): 无法解析位置 0 (1) 处的时间字符串 (16/07/2020):意外字符

转载 作者:行者123 更新时间:2023-12-04 02:33:38 25 4
gpt4 key购买 nike

在我的 Laravel 5.8 应用程序中,我有:config/app.php

'date_format' => 'd/m/Y',
'date_format_js' => 'dd/mm/yy',
模型
use Carbon\Carbon;

class HrHolidayDate extends Model
{
protected $table = 'hr_holiday_dates';

protected $fillable = [
'holiday_name',
'holiday_date',

];

protected $dates = [
'holiday_date'
];


protected $casts = [];


public function setHolidayDateAttribute($input)
{
$this->attributes['holiday_date'] =
Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
}


public function getHolidayDateAttribute($input)
{
return Carbon::createFromFormat('Y-m-d', $input)
->format(config('app.date_format'));
}
}
我尝试在 Blae View 中对其进行格式化,如下所示: view
@foreach($holidays as $key => $holiday)
<td>
{{Carbon\Carbon::parse($holiday->holiday_date)->format('d-m-Y') ?? '' }}
</td>
@endforeach
当我想渲染 View Blade 时,出现此错误:

[2020-07-15 11:57:56] production.ERROR: DateTime::__construct(): Failed to parse time string (16/07/2020) at position 0 (1): Unexpected character (View: C:\xampp\htdocs\laravelapp\resources\views\hr\holiday_dates\index.blade.php) {"userId":466,"exception":"[object] (ErrorException(code: 0): DateTime::__construct(): Failed to parse time string (16/07/2020) at position 0 (1): Unexpected character (View: C:\xampp\htdocs\laravelapp
esources\views\hr\holiday_dates\index.blade.php) at C:\xampp\htdocs\laravelapp\vendor
esbot\carbon\src\Carbon\Traits\Creator.php:81, Exception(code: 0): DateTime::__construct(): Failed to parse time string (16/07/2020) at position 0 (1): Unexpected character at C:\xampp\htdocs\laravelapp\vendor
esbot\carbon\src\Carbon\Traits\Creator.php:81)[stacktrace]


我该如何解决?
谢谢

最佳答案

因为Carbon不懂d/m/Y格式开箱即用。如果你使用斜杠,它应该是 mm/dd/yyyy在这种情况下。 d/m/Y不是官方日期格式。
您可以使用 :

{{ Carbon\Carbon::createFromFormat('d/m/Y', $holiday->holiday_date)->format('d-m-Y') }}

关于laravel - DateTime::__construct(): 无法解析位置 0 (1) 处的时间字符串 (16/07/2020):意外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62913664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com