zazu Posted May 6, 2015 Share Posted May 6, 2015 Hi guys, I'me trying to get the age of a user that i have in my database based on the data type field in database. So in my database in tha data field i have 20.02.1989 Now i want to display the age of the user with a specific ID, i must mention that i want to display the age of all users that will be register in the database. How can i do that because i've tried a lot of things and not of them seems to be working. My code is below: $dateValue = "SELECT EXTRACT(YEAR FROM data_nasterii) FROM bonacuora_clients WHERE id='$id'"; $c=date('Y'); $varsta = $c-$dateValue; Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/ Share on other sites More sharing options...
Psycho Posted May 6, 2015 Share Posted May 6, 2015 (edited) You should first fix the database to change that field to a date type. The database has lots of useful functions and processes for working with dates, but you can't use them on data that is not a date type. Otherwise, your best option is to read the data into PHP, parse it into a date format and then do the calculation. EDIT: My response above is based on what I know date types to by in MySQL and MS SQL. You may be using a different database that stores dates differently. Edited May 6, 2015 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510952 Share on other sites More sharing options...
zazu Posted May 6, 2015 Author Share Posted May 6, 2015 It is a date type Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510953 Share on other sites More sharing options...
Psycho Posted May 6, 2015 Share Posted May 6, 2015 It is a date type Well, you did state data type. What type of database are you using? MySQL and MS SQL do not store dates in the format 20.02.1989, they would use 1989-02-20 Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510954 Share on other sites More sharing options...
zazu Posted May 6, 2015 Author Share Posted May 6, 2015 I use mysql and the date type is 1989-06-03 Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510955 Share on other sites More sharing options...
Barand Posted May 6, 2015 Share Posted May 6, 2015 EG $dob = DateTime::createFromFormat('d.m.Y', '20.02.1989'); $now = new DateTime(); $age = $dob->diff($now)->y; echo $age; // 26 But, as Psycho said, you really should fix your stored date format. You can't even sort by date with your format. Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510956 Share on other sites More sharing options...
zazu Posted May 6, 2015 Author Share Posted May 6, 2015 EG $dob = DateTime::createFromFormat('d.m.Y', '20.02.1989'); $now = new DateTime(); $age = $dob->diff($now)->y; echo $age; // 26 But, as Psycho said, you really should fix your stored date format. You can't even sort by date with your format. Understand, but i want to get the birthday values from database not to specifiy them manually. Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510957 Share on other sites More sharing options...
Psycho Posted May 6, 2015 Share Posted May 6, 2015 Here's a solution that I found via Google. 'dob' represents the Date Of Birth SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age FROM table_name Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1510966 Share on other sites More sharing options...
zazu Posted May 7, 2015 Author Share Posted May 7, 2015 And how the final code should look like? Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1511000 Share on other sites More sharing options...
Solution Barand Posted May 7, 2015 Solution Share Posted May 7, 2015 (edited) Assuming you now have your dates of birth (dob) as type date (yyyy-mm-dd) <?php $mysqli = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); //--------------------------------------------- // Method 1 // Calculate age using PHP //--------------------------------------------- function age($dob) { $dt = new DateTime($dob); return $dt->diff(new DateTime())->y; } $sql = "SELECT name , dob FROM bonacuora_clients ORDER BY name"; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { echo $row['name'] . " - " . age($row['dob']) . '<br/>'; } //--------------------------------------------- // Method 2 // Calculate age using SQL //--------------------------------------------- $sql = "SELECT name , DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age FROM bonacuora_clients ORDER BY name"; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { echo $row['name'] . " - " . $row['age'] . '<br/>'; } ?> Edited May 7, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1511015 Share on other sites More sharing options...
zazu Posted May 8, 2015 Author Share Posted May 8, 2015 Assuming you now have your dates of birth (dob) as type date (yyyy-mm-dd) <?php $mysqli = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); //--------------------------------------------- // Method 1 // Calculate age using PHP //--------------------------------------------- function age($dob) { $dt = new DateTime($dob); return $dt->diff(new DateTime())->y; } $sql = "SELECT name , dob FROM bonacuora_clients ORDER BY name"; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { echo $row['name'] . " - " . age($row['dob']) . '<br/>'; } //--------------------------------------------- // Method 2 // Calculate age using SQL //--------------------------------------------- $sql = "SELECT name , DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age FROM bonacuora_clients ORDER BY name"; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { echo $row['name'] . " - " . $row['age'] . '<br/>'; } ?> Hei Barad, thank you for your reply. The code that you provide in returns me 0 value insted of the age of the user. Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1511141 Share on other sites More sharing options...
Barand Posted May 8, 2015 Share Posted May 8, 2015 The table CREATE TABLE `date_sample` ( `id` int(11) NOT NULL AUTO_INCREMENT, `dob` date NOT NULL, `name` varchar(25) DEFAULT NULL, PRIMARY KEY (`id`) ) The data mysql> SELECT * FROM date_sample; +----+------------+-------+ | id | dob | name | +----+------------+-------+ | 1 | 1945-09-11 | Peter | | 2 | 1949-01-22 | Fred | +----+------------+-------+ My output from the above code METHOD 1 Fred - 66 Peter - 69 METHOD 2 Fred - 66 Peter - 69 Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1511148 Share on other sites More sharing options...
zazu Posted May 8, 2015 Author Share Posted May 8, 2015 The table CREATE TABLE `date_sample` ( `id` int(11) NOT NULL AUTO_INCREMENT, `dob` date NOT NULL, `name` varchar(25) DEFAULT NULL, PRIMARY KEY (`id`) ) The data mysql> SELECT * FROM date_sample; +----+------------+-------+ | id | dob | name | +----+------------+-------+ | 1 | 1945-09-11 | Peter | | 2 | 1949-01-22 | Fred | +----+------------+-------+ My output from the above code METHOD 1 Fred - 66 Peter - 69 METHOD 2 Fred - 66 Peter - 69 Now worked perfectly! It was my bad! Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/296113-calculate-age-from-data-field-in-database/#findComment-1511151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.