Log in

View Full Version : Resolved Birthday calculating script



keyboard
11-02-2011, 12:43 AM
Hey everyone!

I've written this script


<?php
$bday = $_POST['day'];
$bmonth = $_POST['month'];
$byear = $_POST['year'];

$cday = date('j');
$cmonth = date('n');
$cyear = date('Y');

$one = "1";

if($cmonth != "$bmonth"){
if($cmonth > "$bmonth"){
$age = $cyear-$byear;
echo $age;

}else{
if($cmonth < "$bmonth"){
$age = $cyear-$byear-$one;
echo $age;
}
}
}else{
if($cmonth == "$bmonth"){
if($cday != "$bday"){
if($cday > "$bday"){
$age = $cyear-$byear;
echo $age;

}else{
if($cday < "$bday"){
$age = $cyear-$byear-$one;
echo $age;

}
}
}else{
if($cday =="$bday"){
echo "Happy Birthday";
}
}
}
}
?>

to calculate the age of a user and echo their age(for testing perposes).
The problem is I think I mixed up where so of the { and } are ment to go because its echoing either -1 or 0 depening on what I input into the form(whch submits to this script)
The birthay is in this format

2 11 2011
(2nd of november)

Any help?

traq
11-02-2011, 01:31 AM
$now = date_create('now');
$birthdate = date_create('1979-04-11');
$interval = date_diff($now, $birthdate, TRUE);
echo 'I am '.$interval->format('%y years of age');
// outputs "I am 32 years of age" (as of the date of this post :) )
http://php.net/manual/en/book.datetime.php

keyboard
11-02-2011, 09:32 PM
Thanks!