Collecting DOB info and then finding age

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Collecting DOB info and then finding age

Post by Stryks »

Hey all, here is the problem.

I'm collecting DOB information to allow me to track ages of my users (for restricting access to certain areas - its a writing site that allows uers to submit stories, some of which we would like to prevent from being pushed at young readers) but I am a bit stuck with how to go abut it.

Firstly, I want to collect the DOB in a format more like Nov 10, 1975, as the bulk of our users will be Australian (and hence use dd/mm/yy as opposed to mm/dd/yy) so I thought this format would avoid confusion.

So thats fine, I can use strtotime to convert that to a timestamp and then split that into day month and year for use in calculating their age. But of course, I find out now that the timestamp is unable to hold dates earlier than ... I think its 1970 or so.

So what I'm stuck with is the need to somehow find my own way to convert my date format to day, month, and year manually without using strtotime. But the thing that I did like was the fact that strtotime would convert succesfully even if someone used a different date format. I guess I will have to live without this.

Having said all this, I will also be writing this date to a mysql db, so I'm also wondering what the process is there. If I pass the unformatted date across will it translate internally?

Thanks for any help. :)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

maybe this will help?

viewtopic.php?t=17355
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Hey, thanks for the reply. It certainly answers the latter part of the question, although I'm still a bit lost on the most effective way to convert a date in the format 'Nov 10, 1982' to the format '1982-11-10'

Any ideas, keeping in mind that strtotime doesnt work well enough?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

well, i would do something like:

Code: Select all

<?php
$months['Jan'] = "1";
$months['Feb'] = "2";
$months['Mar'] = "3";
$months['Apr'] = "4";
$months['May'] = "5";
$months['Jun'] = "6";
$months['Jul'] = "7";
$months['Aug'] = "8";
$months['Sep'] = "9";
$months['Oct'] = "10";
$months['Nov'] = "11";
$months['Dec'] = "12";
$date = str_replace(",", "", explode(" ", "Nov 10, 1982")); 
$month = $months[$date[0]];
$fixed_date = $date[2]."-".$month."-".$date[1]; 
echo $fixed_date;
?>
didnt test it or anything, its just a thought :)

EDIT
first code didnt work, tested this though, it works.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Hey thanks, the updated post seems to work fine.

I've made two little functions up just in case anyones interested in what I have now.

Code: Select all

function _validateDOB($DOB) &#123;
  //Values must be passed in the format (Oct 21, 2000)
  $months&#1111;'JAN'] = '01';
  $months&#1111;'FEB'] = '02';
  $months&#1111;'MAR'] = '03';
  $months&#1111;'APR'] = '04';
  $months&#1111;'MAY'] = '05';
  $months&#1111;'JUN'] = '06';
  $months&#1111;'JUL'] = '07';
  $months&#1111;'AUG'] = '08';
  $months&#1111;'SEP'] = '09';
  $months&#1111;'OCT'] = '10';
  $months&#1111;'NOV'] = '11';
  $months&#1111;'DEC'] = '12';
		  
  $date = str_replace(",","",explode(" ",$DOB));

  $d = $date&#1111;1];
  $m = $months&#1111;strtoupper($date&#1111;0])]; 
  $y = $date&#1111;2];

  return $y . '-' . $m . '-' . $d;
&#125;

function _dobToAge($DB_DOB) &#123;
  //Values must be passed in mySQL format (yyyy-mm-dd)
  $d = substr($DB_DOB, 8, 2);
  $m = substr($DB_DOB, 5, 2);
  $y = substr($DB_DOB, 0, 4);
  
  if ((date('m') > $m) || ((date('m') == $m) && (date('d') >= $d))) &#123;
    $age = date("Y")-$y;
  &#125; else &#123;
    $age = date("Y")-$y-1;
  &#125;
  return $age;
&#125;
The first is used to translate the date to a format suitable to write to a mySQL date field, and the second is used to convert that date (when pulled from the db afterwards) into the age of the user. At least, thats what its supposed to do. Let me know if you spot anything wrong.

Cheers :D
Post Reply