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

skoobi's avatar
Level 13

Working with multiple date formats

Hi. Ive come stuck where im transferring data from an old database so i can tidy it up, but the dates are a mish mash of formats.

I.e. 18/05/1983 , 29.12.1972 ect.

If i do Carbon::parse($old_date->dob); I get an error that it has failed to parse.

How would i do some sort of check and change the date to a standard date like 'Y-m-d'? Is there a Carbon method that can figure out what format it is and change it?

Cheers

0 likes
2 replies
Cronix's avatar

Is there a Carbon method that can figure out what format it is and change it?

No.

You'd have to do this manually. How much data are you working with? You can check if the date has .'s or /'s and convert it by specifying the format that it needs to parse, but there's also another problem.

Let's say the date was

11/05/1983 or 11.05.1983

How would you know for sure which is the month, and which is the day, if all of the dates aren't stored in a consistent format? If one of the 2 numbers was greater than 12, it's obviously the day, but what if both numbers were <= 12?

skoobi's avatar
Level 13

Cheers for the reply. Ye this is what I thought. I may have to go through the database and check if I can distinguish a pattern.

Please or to participate in this conversation.