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

mvnobrega's avatar

How to convert birth date string to age

When the user registers on the website, the date of birth is stored in a string format, like this: 12/05/1990

I need to calculate the age of the user whenever he submits a new post on the site.

I saw that this function exists: STR_TO_DATE(str, format)

But I can't use it in laravel, and I also don't know how I could get the age using STR_TO_DATE(str, format)

How can I do this ?

0 likes
5 replies
MichalOravec's avatar
Level 75

Laravel use Carbon so it's really easy with this

use Carbon\Carbon;

$age = Carbon::parse('12/05/1990')->age;
1 like
mvnobrega's avatar

@michaloravec

Thanks for the sugestion. But because it is a multi-language site, the date format can also change. May be 05/12/1990 or 05/12/1990

How could I configure this?

MichalOravec's avatar

If you use a datepicker you can use hidden input where you store expected format of date. And then you will just use date from that hidden input.

1 like
automica's avatar

@mvnobrega you would be best to convert the DOB based on their region to ISO and parse that.

carbon wont know if you are talking about 5th December 1990 or 12th May 1990 otherwise.

1 like

Please or to participate in this conversation.