1 頁 (共 1 頁)

Carbon Data Missing

發表於 : 2017-12-13 16:49:59
yehlu
https://stackoverflow.com/questions/349 ... ta-missing


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.

Explanation

The Problem

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 will throw:

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 will throw:

InvalidArgumentException in Carbon.php line 425:
Trailing data