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
Release: 2024-11-28 16:14:12
Original
224 people have browsed it

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;
Copy after login

Object-oriented

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

MySQL

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template