I don't program in PHP so looking at similar code and hacking it has not worked well. Perhaps someone here can help me get started.

I'm a genealogist and would like a simple script to determine when someone was born based on their age on a particular date. For example, an 1890 census was conducted on 28 April 1891 and the head of household states that he is 75 years old. I would like to enter the age and the census date. Script would return a response something like...

"If the person is 75 years old on the 28 April 1891 then
he would have been born between 29 April 1815 and 28 April 1816."

This would be a very helpful tool for those using census records to research their family history.

Thanks

Paarade

$age = 75;
$current_year = 1891;
$the_day_you_born = $current_year - $age; //minus the age in the current year

what you have given is your requirement, but how you want it to work,
1) do you want single php form where you enter age and census year and you get period of year of born
2) or you have some large data in excel, csv and you want to add one more column to that table

Happytogether

This would only give me the year of birth. I was wanting to be more specific about the possible range of dates that could be possible birthdates. My code got sloppy when I tried to adjust for end of month entries and leap years.

urtrivedi

I would like the first option of a single form where I could enter age and census year. Something like http://easycalculation.com/date-day/age-calculator.php

This page does the calculation. It uses DateInterval so requires PHP 5.3.0 or later.

<?php

$age = $_GET["age"];
$year = $_GET["year"];
$month = $_GET["month"];
$day = $_GET["day"];

if (!$_GET["age"]) {
  }
elseif (!checkdate($month,$day,$year)) {
  echo "Invalid Date";
  }
else {
  $one_year = new DateInterval("P1Y");
  $one_day = new DateInterval("P1D");
  $age_years = new DateInterval("P".$age."Y");
  $min_date = new DateTime(sprintf("%04d",$year)."-".sprintf("%02d",$month)."-".sprintf("%02d",$day),new DateTimeZone("Pacific/Auckland"));
  $min_date -> sub($age_years);
  $min_date -> sub($one_year);
  if ( ($min_date -> format("j") == $day) && ($min_date -> format("n") == $month) ) {
    $min_date -> add($one_day);
    }
  $max_date = new DateTime($year."-".$month."-".$day,new DateTimeZone("Pacific/Auckland"));
  $max_date -> sub($age_years);
  if ( ($max_date -> format("j") != $day) || ($max_date -> format("n") != $month) ) {
    $max_date -> sub($one_day);
    }
  echo '<p>If the person was <b>'.$age.'</b> on <b>'.sprintf("%04d",$year).'-'.sprintf("%02d",$month).'-'.sprintf("%02d",$day).'</b>, they must have been born between <b>'.$min_date->format("Y-m-d").'</b> and <b>'.$max_date->format("Y-m-d").'</b>.</p>';
  }
$months = array(1=>'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
echo '<hr>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="get">';
echo '<p>Age: <input type="text" name="age" maxlength="3" style="width: 30px;"></p>';
echo '<p>Year: <input type="text" name="year" maxlength="4" style="width: 40px;"> Month: <select name="month"><option selected value="0"></option>';
for ($i=1;$i<=12;$i++) {
  echo '<option value="'.$i.'">'.$months[$i].'</option>';
  }
echo '</select> Day: <select name="day"><option selected value="0"></option>';
for ($i=1;$i<=31;$i++) {
  echo '<option value="'.$i.'">'.sprintf("%02d",$i).'</option>';
  }
echo '</select></p>';
echo '<p><input type="submit" value="Calculate"></p>';
echo '</form>';

?>

And if you don't have PHP 5.3.0, you can achieve the same thing with this code:

<?php

$age = $_GET["age"];
$year = $_GET["year"];
$month = $_GET["month"];
$day = $_GET["day"];

if (!$_GET["age"]) {
  }
elseif (!checkdate($month,$day,$year)) {
  echo "Invalid Date";
  }
else {
  $min_date_year = $year - $age - 1;
  $min_date = (checkdate($month,$day,$min_date_year)) ? mktime(0,0,0,$month,$day,$min_date_year)+86400 : mktime(0,0,0,3,1,$min_date_year);
  $max_date_year = $year - $age;
  $max_date = (checkdate($month,$day,$max_date_year)) ? mktime(0,0,0,$month,$day,$max_date_year) : mktime(0,0,0,2,28,$max_date_year);
  echo '<p>If the person was <b>'.$age.'</b> on <b>'.sprintf("%04d",$year).'-'.sprintf("%02d",$month).'-'.sprintf("%02d",$day).'</b>, they must have been born between <b>'.date("Y-m-d",$min_date).'</b> and <b>'.date("Y-m-d",$max_date).'</b>.</p>';
  }
$months = array(1=>'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
echo '<hr>';
echo '<form action="'.$_SERVER["PHP_SELF"].'" method="get">';
echo '<p>Age: <input type="text" name="age" maxlength="3" style="width: 30px;"></p>';
echo '<p>Year: <input type="text" name="year" maxlength="4" style="width: 40px;"> Month: <select name="month"><option selected value="0"></option>';
for ($i=1;$i<=12;$i++) {
  echo '<option value="'.$i.'">'.$months[$i].'</option>';
  }
echo '</select> Day: <select name="day"><option selected value="0"></option>';
for ($i=1;$i<=31;$i++) {
  echo '<option value="'.$i.'">'.sprintf("%02d",$i).'</option>';
  }
echo '</select></p>';
echo '<p><input type="submit" value="Calculate"></p>';
echo '</form>';

?>

Thanks EdwinHermann

Obviously my webprovider does not support PHP 5.3.0 (cPanel says it is PHP 5.2.17)

sandbox.quoddy.ca/edwinhermann0.php

so I have used the code that does not take advantage of DateInterval

sandbox.quoddy.ca/edwinhermann.php

seems to work fine. First 30 or 40 entries seem to calculate correctly. Thanks very much for the useful tool.

Paarade

Member Avatar for diafol

ha ha ha Ed, immortalised in a sandbox :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.