Carbon: 馃悰 Parsing date string (2.41.0) not correct

Created on 6 Oct 2020  路  4Comments  路  Source: briannesbitt/Carbon


Hello,

I encountered an issue with the following code:

echo Carbon::parse('20201006')->toDateTimeString();

Carbon version: 2.41.0

PHP version: 7.3.22 & 7.2.33

I expected to get:

2020-10-06 00:00:00

But I actually get:

1970-08-22 19:23:26

On 2.40.1 parsing date without special chars was correct.

Thanks!

bug

Most helpful comment

This format is supported again in 2.41.1 in parse() and the constructor.

I still recommend to use more explicit formats (- separator for dates, : for times) and use createFromFormat() when handling non-ISO formats.

For those who handle timestamps, the dedicated createFromTimestamp() or createFromTimestampUTC() are the explicit way to create an instance from timestamp (without the new minimum of 99991231).

All 4 comments

We are experiencing the exact same thing as @mrcnpdlk since the upgrade to 2.41.0 yesterday, breaking code.

Reverting to 2.40.1 solves the issue for now.

Hello,

Pure digits string is interpreted as a timestamp. You also can work around with:

Carbon::createFromFormat('Ymd', '20201006')

I will check if we can keep this feature somehow.

Thanks

As the parse previously supported this format up to year 9999. and as 99991231 is now a very old timestamp. It would be fine to keep the Ymd parsing below this limit.

This format is supported again in 2.41.1 in parse() and the constructor.

I still recommend to use more explicit formats (- separator for dates, : for times) and use createFromFormat() when handling non-ISO formats.

For those who handle timestamps, the dedicated createFromTimestamp() or createFromTimestampUTC() are the explicit way to create an instance from timestamp (without the new minimum of 99991231).

Was this page helpful?
0 / 5 - 0 ratings