Reputation: 61
Hi i have a site with users displayed on the front page. When users sign up to the site they sign up with their date of birth. This is stored in my database.
I have pulled the users from the database and have their location and name displaying.
I am also trying to add their age which is calculated from their date of birth. But at the moment it won't work, the users age isn't being displayed.
Can anyone help me find where i'm going wrong?
Code:
<?php
$dob = $platinum['dob'];
function age_from_dob($dob) {
list($y,$m,$d) = explode('-', $dob);
if (($m = (date('m') - $m)) < 0) {
$y++;
} elseif ($m == 0 && date('d') - $d < 0) {
$y++;
}
return date('Y') - $y;
}
$dob = age_from_dob($dob);
?>
<?
$platinum_set = get_platinum_users();
while ($platinum = mysql_fetch_array($platinum_set)) {
echo"
<div class=\"platinumcase\">
<a href=\"profile.php?id={$platinum['id']}\"><img width=80px height= 80px src=\"data/photos/{$platinum['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$platinum['first_name']} {$platinum['last_name']}</h58><br/><br/><h52>{$platinum['$dob']}<br/><br/>{$platinum['location']}</h52>
</div>";
}
?>
Upvotes: 0
Views: 1552
Reputation: 16
function age_from_dob($dob) {
return DateTime::createFromFormat('Y-m-d',$dob, $tz)->diff(new DateTime('now', $tz))->y;
}
Upvotes: 0
Reputation: 1416
I use this function to return age of my user. Hope this helps
function HowOld($DOB)
{
if($DOB)
{
$birthday = strtotime($DOB);
$date = strtotime(date("Y-m-d "));
$difference = $date - $birthday;
$years = $difference/(60 * 60 * 24 * 365);
$value = floor($years);
return $value;
}
return false;
}
Upvotes: 0
Reputation: 1703
i dont know which templating engine you use and what is allowd and what not... but normally the easiest way is the best ... so try this
<?php
$dob = $platinum['dob'];
function age_from_dob($dob) {
$dob = '1999-12-03';
$age = date_diff(date_create($dob), date_create('now'))->y;
return $age;
}
?>
<?
$platinum_set = get_platinum_users();
while ($platinum = mysql_fetch_array($platinum_set)) {
$age = age_from_dob($platinum['dob']);
echo "
<div class=\"platinumcase\">
<a href=\"profile.php?id={$platinum['id']}\"><img width=80px height= 80px src=\"data/photos/{$platinum['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$platinum['first_name']} {$platinum['last_name']}</h58><br/><br/><h52> ".$age." <br/><br/>{$platinum['location']}</h52>
</div>";
}
?>
Upvotes: 1