Jump to content

Help: Converting Date to Age


Rottingham

Recommended Posts

SELECT (YEAR(CURDATE())-YEAR('1978-02-03'))-(RIGHT(CURDATE(),5)<RIGHT('1978-02-03',5)) as age;

if you have a column birthdate in table t, use like this:

SELECT (YEAR(CURDATE())-YEAR(birthdate))-(RIGHT(CURDATE(),5)<RIGHT(birthdate,5)) from t  as age;

Thank you for the help on this. I'm sorry I didn't communicate very well! Like I said, it was 2am.

 

I am looking to see how it is done outside of the query. I.e I have already pulled the data, and at some point later I want to take $DOB and do something like get_age($DOB);

Actually, I am the one who doesn't read well.

it is similar:

<?php
$birth = '1975-02-05';
$now = date('Y-m-d'); // get today's year and date

$age = substr($now, 0, 4) - substr($birth, 0, 4);
if (substr($birth,-5) > substr($now,-5)) // if birthdate later than today's date
    $age -= 1;

echo $age;
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.