briannesbitt / Carbon

A simple PHP API extension for DateTime.
https://carbon.nesbot.com/
MIT License
16.56k stars 1.28k forks source link

CarbonInterval::fromString parsing error #2678

Closed OmnibladeZ closed 2 years ago

OmnibladeZ commented 2 years ago

Hello,

I encountered an issue with the following code:

echo CarbonInterval::fromString('1yr 1mo 3w 3d 14h 23m 57s');

Carbon version: 2.41.5

PHP version: 7.2.34

I expected to get:

1 year 1 month 3 weeks 3 days 14 hours 23 minutes 57 seconds

But I actually get:

Carbon\Exceptions\InvalidIntervalException
Invalid part 1yr in definition 1yr 1mo 3w 3d 14h 23m 57s

Thanks!

My work-around at the moment is to add case for 'yr, yrs and mos' to the CarbonInterval fromString() function. The time string '1yr 1mo 3w 3d 14h 23m 57s' was generated from CarbonInterval using the forHumans() function with ['short' => true]. I searched the file CarbonInterval looking for 'yr' and 'mos' as well to see where those came from but there are no hits for those.

kylekatarnls commented 2 years ago

Hello,

Indeed yr should be added as an alias for year.

PR for this would be welcome. Else I'll try to fix it for next release.