PHPBuilder - Age,Weekday calculator.



RSS Twitter
Snippets Calendars Dates

Age,Weekday calculator.

by: Nageswara Rao Devarakonda
|
June 27, 2002

Version: 10.0

Type: Full Script

Category: Calendars/Dates

License: Other

Description: The file takes a date of birth like dd-mm-yyyy or dd/mm/yyyy and calculates the number of years and days passed by and gives the weekday. The best part is it works for any year from after death of christ to till date. even the leap years are also considered



<?
function dow($dd,$mm,$yy)
{
	$res=($yy+(int)($yy-1)/4-(int)($yy-1)/100+(int)($yy-1)/400+julianday($dd,$mm,$yy))%7;
	if($res==1)
		$res="Sunday";
	elseif($res==2)
		$res="Monday";
	elseif($res==3)
		$res="Tuesday";
	elseif($res==4)
		$res="Wednesday";
	elseif($res==5)
		$res="Thursday";
	elseif($res==6)
		$res="Friday";
	else
		$res="Saturday";	
	return($res);
}
function julianday($dd,$mm,$yy)
{
	$days=array(31,0,31,30,31,30,31,31,30,31,30,31);
	$days[1]=$yy%4==0&&$yy%100!=0||$yy%400==0?29:28;
	for ($i=1;$i<$mm;$i++)
		$jd=$jd+$days[$i-1];
	return($jd+$dd);
}
function age($dd,$mm,$yy)
{
	$now=split("-",date("d-m-Y"));
	$dd1=$now[0];
	$mm1=$now[1];
	$yy1=$now[2];
	if($yy1==$yy)
	{
		$age["years"]=0;
		$age["days"]=julianday($dd1,$mm1,$yy1)-julianday($dd,$mm,$yy);
		return($age);
	}
	else
	{
	$ans1=$yy%4==0&&$yy%100!=0||$yy%400==0?366-julianday($dd,$mm,$yy):365-julianday($dd,$mm,$yy);
		$ans2=julianday($dd1,$mm1,$yy1);
		$sy=$yy+1;
		$ly=$yy1-1;
		while($sy++<=$ly)
			$sum=$sy%4==0&&$sy%100!=0||$sy%400==0?$sum+366:$sum+365;
	}
	$years=(float)($sum+$ans1+$ans2)/365;
	$days=($years-(int)($years))*365;
	$age[years]=(int)$years;
	$age[days]=(int)$days;
	return ($age);
}
?>
<html><head><title>Date functions</title></head><body><TABLE><FORM method=post><TR><Td>Enter Date</Th><TD><INPUT name=dob></TD></TR>	<TR><Th><INPUT type=submit></Th></TR></FORM></TABLE></body>
</html>

<?
if($dob)
{
	$dob=str_replace(" ","",$dob);
	if(!ereg("^[0-9]{1,2}(-|/)[0-9]{1,2}(-|/)[0-9]{4}$",$dob))
		$msg="Invalid date";
	elseif(ereg("^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}",$dob))
		$dob=split("/",$dob);
	else
		$dob=split("-",$dob);
	if($msg) die($msg);
	$dd=$dob[0];
	$mm=$dob[1];
	$yy=$dob[2];
	$age=age($dd,$mm,$yy);
	echo "The date you entered is <b>$dd-$mm-$yy</b><br>";
	echo "The weekday is <B>" . dow($dd,$mm,$yy) . "</b><br>";
	echo "Date difference or age is  <B>$age[years] years and $age[days] days </b><br>";
}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Nageswara Rao Devarakonda

Comment:



Comment:

(Maximum characters: 1200). You have characters left.