Some web applications are needed to show the age of the user. In that case, you need to calculate the user age from date of birth. Here we’ll provide a short PHP code snippet to calculate age from date of birth.
In the following code, date()
, date_create()
, and date_diff()
functions are used to calculate age of the user till today in PHP.
$dateOfBirth = "17-10-1985";
$today = date("Y-m-d");
$diff = date_diff(date_create($dateOfBirth), date_create($today));
echo 'Age is '.$diff->format('%y');
Use the above code to get the age from date of birth using PHP date functions.
$dateOfBirth = “01-01-2020”;
$today = date(“Y-m-d”);
$diff = date_diff(date_create($dateOfBirth), date_create($today));
echo ‘Age is ‘.$diff->format(‘%y’);
Thanks for sharing . This code work.
Very nice sir, your tutorials are always helpful for me thank you !
Thanks for sharing . This code work. God bless you.
this script is perfect
the script is very good.. working also, thanks
great
Hi,
I have a date text box
how can the age get calculated and appear in Text Box so that it gets saved in database.
Thank’s a lot I’m trying the script is working, Could you calculate age using Form?
I am really really thank full to you for helping me in age calculator
great it works for me
Awesome!!
It works (Y)
Thanks a lot. It’s easy to understand And it’s working too.
Awesome it’s working dude thanks
Great working. Thanks
Good code easy to understand
tested working. thank you
I spent a lot on this , thanks a lot … It is very helpful .
Your are making my learning awesome … Thank you Codexworld Creater….
I am really really thank full to you for helping me in login with fb
the script is too short.. working also, thanks