Solved

calculate age from dob??

Posted on 2009-04-10
6
341 Views
Last Modified: 2013-12-12
Below is how i store a dob... by using echo $getuserprofile['dob']; i can echo a users dob...but who can i work out there age?
<?php  
	$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$years = range (1910, 2015);
 
//**********************************************
 
echo "Day: <select name='day'>";
foreach ($days as $value) {
   echo '<option ';
   if($splitDate[0] == $value)
      echo 'selected="selected"';
   echo ' value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';
 
echo "Month: <select name='month'>";
foreach ($months as $value) {
 
   echo '<option ';
   if($splitDate[1]==$value) 
      echo "selected='selected'";
   echo 'value="'.$value.'">'.$value.'</option>\n';
} echo '</select>';
 
echo "Year: <select name='year'>";
foreach ($years as $value) {
   echo '<option ';
   if($splitDate[2]==$value)
      echo 'selected="selected"';
   echo " value='".$value."'>".$value."</option>\n";
} 
$dob = $day.'-'.$month.'-'.$year; 
 ?>

Open in new window

0
Comment
Question by:runnerjp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24116607
try this
$today=getdate();
$age=$today['year']-$dob_year-1;
// now have they had a birthday this year?
if ($today['month']>$dob_month)
  $age++;
else
  if ($today['month']==$dob_month)
    if ($today['mday']>=$dob_day)
      $age++;
 
echo $age;

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24116680

<?php // RAY compute_age_from_dob.php
 
// TEST DATA
$dob = '15'.'-'.'09'.'-'.'1950';
 
// GET TIMESTAMPS
$ts_dob = strtotime($dob);
$ts_now = time();
 
// DIFFERENCE IN YEARS
$years = date('Y', $ts_now) - date('Y', $ts_dob);
 
// IF BEFORE THE BIRTHDAY
$mdnow = date('m-d', $ts_now);
$mddob = date('m-d', $ts_dob);
if ($mdnow < $mddob) $years--;
 
// SHOW THE ANSWER
echo $years;

Open in new window

0
 

Author Comment

by:runnerjp
ID: 24116846
soryy i should have said i display dob as 19-December-1987   so would i need to somehow convert the month into numeric?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24116858
That should work OK with strtotime()

http://www.laprbass.com/RAY_strtotime.php?s=19-December-1987

HTH, ~Ray
0
 
LVL 19

Expert Comment

by:Michael701
ID: 24117010

// since you already $months, this should get the month number
$dob_month = array_keys ( $months, $dob_month_text );
 
 
// but really you should
 
echo "Month: <select name='month'>";
foreach ($months as $month_number=>$month_name) {
 
   echo '<option ';
   if($splitDate[1]==$month_name) 
      echo "selected='selected' ";
   echo 'value="'.$month_number.'">'.$month_name.'</option>\n';
} echo '</select>';

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24117044
One note, not directly at issue here, but useful as a "best practices" approach to handling dates.  Make all internal representations into ISO8601 date/time strings.  php date('c') gives you the traditional format

YYYY-MM-DD HH:MM:SS

This is useful because you can use it with strtotime and date() to format dates and times for output, and you can see the columns line up in reports.  Also, it collates and compares correctly whether in a timestamp or a character string.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question