This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
In this article, you will learn how to calculate age based on birthdate in PHP.
Let’s say you have 2 string variables containing date of birth and current date.
$birthDate = "17-10-1998";
$currentDate = date("d-m-Y");
Calculate Age
In order to calculate age based on birthdate, you can use the date_diff()
method and date_create()
method.
$birthDate = "17-10-1998";
$currentDate = date("d-m-Y");
$age = date_diff(date_create($birthDate), date_create($currentDate));
echo "Current age is ".$age->format("%y");
// Current age is 22
Note: The date_diff()
method functions by calculating the mathematical difference of two supplied dates. The date_create()
method functions by creating a DateTime object from a supplied string.
The post How to Calculate Age in PHP appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-03-02T09:25:43+00:00) How to Calculate Age in PHP. Retrieved from https://www.scien.cx/2021/03/02/how-to-calculate-age-in-php/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.