Does anyone know how i can calculate my age and remaining days with MySQL, taking into account of leap years?
I found examples which can calculate the total number of days.
But i want a result like:
22 years, 134 days,
23 years, 361 days
24 years, 11 days
For now i got this:
SELECT
name,
dob,
TIMESTAMPDIFF( YEAR, dob, now() ) as _year,
TIMESTAMPDIFF( MONTH, dob, now() ) % 12 as _month,
FLOOR( TIMESTAMPDIFF( DAY, dob, now() ) % 30.4375 ) as _day
FROM
client
But this doesn't work and % 30.4375 is not a solution for leap years.
MySQL is a primarily a database and not and render engine. I would suggest using the PHP DateTime Object to handle the timespan - after loading the data from the MySQL server:
$d1=new DateTime($row['dob']);
$d2=new DateTime('now');
$diff=$d2->diff($d1);
print_r( $diff ) ;
How about this :
SELECT
name,
dob,
TIMESTAMPDIFF(YEAR, dob, now()) years,
TIMESTAMPDIFF(DAY, TIMESTAMPADD(YEAR, TIMESTAMPDIFF(YEAR, dob, now()), dob), now()) days
FROM
client;
First part gets the years, second part adds years to date of birth to get last birthday & then gets number of days since then.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.