| 
	
 | 
 Posted by Rob on 06/18/07 10:28 
Rob wrote: 
> This is my first attempt at php. 
>  
> I have cobbled this code from google searches and placed it in a file  
> maxage.php.   I want to calculate how old my son is with a php script. 
>  
> I do not seem to be able to call the CalcAge function. 
> ---------------------------------------------------------------- 
> <html> 
> <head><basefont face="Arial"></head> 
> <body> 
>  
> <?php 
> function CalcAge($date_of_birth) { // YYYY-MM-DD 
> $cur_year=date("Y"); 
> $cur_month=date("m"); 
> $cur_day=date("d"); 
>  
> $dob_year=substr($date_of_birth, 0, 4); 
> $dob_month=substr($date_of_birth, 5, 2); 
> $dob_day=substr($date_of_birth, 8, 2); 
>  
> if($cur_month>$dob_month || ($dob_month==$cur_month && 
> $cur_day>=$dob_day) ) 
> return $cur_year-$dob_year; 
> else 
> return $cur_year-$dob_year-1; 
> } 
> ?> 
>  
> <?php 
> echo '<h2>CalcAge(2005-10-07)</h2>'; 
>  
> ?> 
>  
> </body> 
> </html> 
> --------------------------------------------------------------------- 
>  
> Thanks for your help 
 
 
A bit closer, this is the start of working system. 
 
I would like to have a system that tells me the age in  years and months. 
 
At he moment this code tells me that my son is over 1 year of age. 
 
-------------------------------------------------------------------------- 
<html> 
<head><basefont face="Arial"></head> 
<body> 
 
<?php 
function CalcAge($date_of_birth) { // YYYY-MM-DD 
$cur_year=date("Y"); 
$cur_month=date("m"); 
$cur_day=date("d"); 
 
$dob_year=substr($date_of_birth, 0, 4); 
$dob_month=substr($date_of_birth, 5, 2); 
$dob_day=substr($date_of_birth, 8, 2); 
 
if($cur_month>$dob_month || ($dob_month==$cur_month && 
$cur_day>=$dob_day) ) 
return $cur_year-$dob_year; 
else 
return $cur_year-$dob_year-1; 
} 
?> 
 
 
 
<?php 
$output = CalcAge('2005-10-07'); 
echo "<h2> $output </h2>"; 
?> 
 
</body> 
</html> 
 
---------------------------------------------------------------------------------
 
[Back to original message] 
 |