...

View Full Version : Age Calculation



tyanque
12-30-2007, 12:33 AM
Hey, Ive found a script on the internet which takes a date of birth and changes it to an age. How would I change the code so that the dob is dynamic using $_POST from user input?

Here is the code:

<?php
$dob = "22-04-1989";
$date_now = date("d-m-Y");

$ex = explode("-", $dob);
$ex1 = explode("-", $date_now);

$age = $ex1[2] - $ex[2];

if($ex[1] > $ex1[1])
{
$age--;

} elseif($ex[1] == $ex1[1] && $ex[0] > $ex1[0])
{
$age--;
}
echo $age;
?>

thanks :) hope you understand what i mean :p

oesxyl
12-30-2007, 12:41 AM
<?php
$dob = $_POST['your name attribute from form'];
$date_now = date("d-m-Y");

$ex = explode("-", $dob);
$ex1 = explode("-", $date_now);

$age = $ex1[2] - $ex[2];

if($ex[1] > $ex1[1])
{
$age--;

} elseif($ex[1] == $ex1[1] && $ex[0] > $ex1[0])
{
$age--;
}
echo $age;
?>

PS: there are some checking you must do before, I explain only how to pass parameters from form

best regards

tyanque
12-30-2007, 12:45 AM
thanks :) but I forgot 2 mention 1 thing sorry :s. Theres 3 field elements 1 for day, 1 for month and 1 for year. How would I do it getting data from 3 fields?

oesxyl
12-30-2007, 01:00 AM
thanks :) but I forgot 2 mention 1 thing sorry :s. Theres 3 field elements 1 for day, 1 for month and 1 for year. How would I do it getting data from 3 fields?

same way for each field, but change the name in the $_POST variable, :)



<form method="post" action="age.php">
<input type="text" name="month"/>
<input type="text" name="day"/>
<input type="text" name="year"/>
<input type="submit" name="ok" value="OK"/>
</form>


age.php


$day = $_POST['day'];
$month = $_POST['month'];
$yesr = $_POST['year'];


as in previos post no checking

A better way is to put all in one file, see example #3 from this link:

http://www.php.net/manual/en/language.variables.external.php

best regards

tyanque
12-30-2007, 01:02 AM
thanks :) ill take a look into that fingers crossed it will work :p

oesxyl
12-30-2007, 01:09 AM
thanks :) ill take a look into that fingers crossed it will work :p

if you have problems, just post them here with details, :)
And read the security section from the manual, it's critic.

http://www.php.net/manual/en/

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum