Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

knubbe's avatar
Level 36

Create date from string 'm.Y'

I have a problem with creating date from string with Carbon. For example I have a string 02.2022 which represent month (February) and year but when I use

Carbon::createFromFormat('m.Y', $date)

I store 2022-03-03 :(

How can I prevent this behaviour or at least to store 2022-02-01?

0 likes
2 replies
Braunson's avatar
Braunson
Best Answer
Level 18

Try this:

Carbon::createFromFormat('!m.Y', '02.2022')

Which returns 2022-02-01

More information on the ! character here.

Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values ( 0 for hour, minute, second and fraction, 1 for month and day, 1970 for year and UTC for timezone information)

Without !, all fields will be set to the current date and time.

1 like

Please or to participate in this conversation.