Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createFromFormat() weird output with format 'Y-m' in february #2930

Closed
Mehdimed opened this issue Jan 31, 2024 · 1 comment
Closed

createFromFormat() weird output with format 'Y-m' in february #2930

Mehdimed opened this issue Jan 31, 2024 · 1 comment
Labels
duplicate Solution/explanation on this issue has already been given in an other issue

Comments

@Mehdimed
Copy link

Mehdimed commented Jan 31, 2024

Hello,

I encountered an issue with the following code:

echo Carbon::createFromFormat('Y-m', '2023-02');

Carbon version: 2.72.2

PHP version: 8.2

https://play.phpsandbox.io/nesbot/carbon?version=3.0.0.0-beta3&input=%3C%3Fphp%0A%0Aecho%20Carbon%3A%3AcreateFromFormat(%27Y-m%27%2C%20%272023-02%27)%3B

I expected to get:

2023-02-28 12:14:58

But I actually get:

2023-03-03 12:14:58

i'm trying to guess what's going on here, by default the createFromFormat() method when used with the 'Y-m' format, will add 30 or 31 as days and as there's only 28 days in february , the date become march the 3rd or march the 2nd during leap year.

Thanks!

(I haven't time to check if this issue has already been opened , and sorry if it's normal behavior and just me that did not read correctly the documentation)

@kylekatarnls kylekatarnls added the duplicate Solution/explanation on this issue has already been given in an other issue label Jan 31, 2024
@kylekatarnls
Copy link
Collaborator

Hello,

This behavior is directly inherited from DateTime::createFromFormat('Y-m', '2023-02')

Use '!Y-m' to reset the day (see PHP doc for operator explanation: https://www.php.net/manual/en/datetimeimmutable.createfromformat.php)

Duplicates #1003 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Solution/explanation on this issue has already been given in an other issue
Projects
None yet
Development

No branches or pull requests

2 participants