Home >Database >Mysql Tutorial >How Can I Calculate Age from a Date of Birth Using PHP and MySQL?

How Can I Calculate Age from a Date of Birth Using PHP and MySQL?

Linda Hamilton
Linda HamiltonOriginal
2024-11-28 16:14:12341browse

How Can I Calculate Age from a Date of Birth Using PHP and MySQL?

Calculating Age Based on Date of Birth

The provided MySQL query fetches user details, including their birth dates. To determine their ages, we can utilize various approaches.

PHP

Procedural

$dob = "1999-03-15";
$age = date_diff(date_create($dob), date_create('today'))->y;

Object-oriented

$dob = new DateTime("1999-03-15");
$today = new DateTime('today');
$age = $dob->diff($today)->y;

MySQL

SELECT TIMESTAMPDIFF(YEAR, '1999-03-15', CURDATE()) AS age

The above is the detailed content of How Can I Calculate Age from a Date of Birth Using PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn