Laravel Carbon 数据丢失 [英] Laravel Carbon Data Missing

查看:7

问题描述

在我的模型中,我有以下内容:

In my model I have the following:

protected $dates = [
    'start',
    'end',
    'created_at',
    'updated_at'
];

我正在使用日期时间选择器插入开始和结束日期,格式如下:

I am using a datetime picker to insert the start and end dates, in this format:

2016-01-23 22:00

没有秒.当我这样做时,我得到了这个错误:

Without the seconds. When I do it like this, I get this error:

InvalidArgumentException in Carbon.php line 425:
Data missing
at Carbon::createFromFormat('Y-m-d H:i:s', '2016-01-23 22:00') in Model.php line 3015

如果我确实包含秒数,它会起作用.秒对我来说并不重要,我不想将它们包含在我的日期时间选择器字段中.有什么办法可以让我仍然可以将这些字段用作日期字段?

If I do include the seconds, it works. The seconds are not important to me, and I do not want to include them in my datetime picker fields. Any way around this so I can still use those fields as date fields?

推荐答案

tl;dr

您的日期字符串和您的日期格式不同,您必须更改格式字符串或修改日期字符串以使其匹配.

tl;dr

Your date string and your date format is different, you have to change the format string or modify the date string so they match.

当 Carbon 的 createFromFormat 函数接收到与传递的格式字符串不匹配的日期字符串时,会出现此错误.更准确地说,这来自 DateTime::createFromFormat函数,因为 Carbon 只是调用它:

This error arises when Carbon's createFromFormat function receieves a date string that doesn't match the passed format string. More precisely this comes from the DateTime::createFromFormat function, because Carbon just calls that:

public static function createFromFormat($format, $time, $tz = null)
{
    if ($tz !== null) {
        $dt = parent::createFromFormat($format, $time, static::safeCreateDateTimeZone($tz));
    } else {
        $dt = parent::createFromFormat($format, $time); // Where the error happens.
    }

    if ($dt instanceof DateTime) {
        return static::instance($dt);
    }

    $errors = static::getLastErrors();
    throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors'])); // Where the exception was thrown.
}

数据不足

如果您的日期字符串比这种情况下的格式字符串短":

Not enough data

If your date string is "shorter" than the format string like in this case:

Carbon::createFromFormat('Y-m-d H:i:s', '2017-01-04 00:52');

碳会抛出:

Carbon.php 第 425 行中的 InvalidArgumentException:
数据缺失

InvalidArgumentException in Carbon.php line 425:
Data missing

数据过多

如果您的日期字符串比这种情况下的格式字符串长":

Too much data

If your date string is "longer" than the format string like in this case:

 Carbon::createFromFormat('Y-m-d H:i', '2017-01-02 00:27:00');

碳会抛出:

Carbon.php 第 425 行中的 InvalidArgumentException:
追踪数据

InvalidArgumentException in Carbon.php line 425:
Trailing data

引擎盖下

根据 关于 mutators 的文档,默认日期格式为:'Ymd H:i:s'.日期处理发生在 Model 的 asDateTime 函数.在最后一个条件下 getDateFormat 函数 被调用,这就是自定义格式的来源.默认格式为 在数据库的 中定义语法.

Under the hood

According to the documentation on mutators the default date format is: 'Y-m-d H:i:s'. The date processing happens in the Model's asDateTime function. In the last condition the getDateFormat function is called, thats where the custom format comes from. The default format is defined in the Database's Grammar class.

您必须确保日期字符串与格式字符串匹配.

You have to make sure that the date string matches the format string.

您可以像这样覆盖默认格式字符串:

You can override the default format string like this:

class Event extends Model {
    protected $dateFormat = 'Y-m-d H:i';
}

这种方法有两个问题:

  • 这将适用于模型的 $dates 数组中定义的每个字段.
  • 您必须以这种格式将数据存储在数据库中.
  • This will apply to every field defined in the model's $dates array.
  • You have to store the data in this format in the database.

我推荐的解决方案是日期格式应该保持默认的 'Y-m-d H:i:s' 并且您应该填写日期的缺失部分,如下所示:

My recommended solution is that the date format should stay the default 'Y-m-d H:i:s' and you should complete the missing parts of the date, like this:

public function store(Request $request) {
    $requestData = $request->all();
    $requestData['start_time'] .= ':00';
    $requestData['end_time'] .= ':00';
    $event = new Event($requestData);
    $event->save();
}

当你想使用日期时,你应该格式化它:

And when you want to use the date you should format it:

public function show(Request request, $eventId) {
    $event = Event::findOrFail($eventId);
    $startTime = $event->start_time->format('Y-m-d H:i');
    $endTime = $event->end_time->format('Y-m-d H:i');
}

当然,字段应该被修改为日期:

Of course the fields should be mutated to dates:

class Event extends Model {
    protected $dates = [
        'start_time',
        'end_time',
        'created_at',
        'updated_at',
        'deleted_at',
    ];
}
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆