0

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

5
Contributors
6
Replies
10
Views
6 Years
Discussion Span
Last Post by diafol
0
$age = 75;
$current_year = 1891;
$the_day_you_born = $current_year - $age; //minus the age in the current year
0

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

Edited by urtrivedi: n/a

0

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

1

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>';

?>

Edited by edwinhermann: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.