pTutorial

New way to learn PHP Example

PHP Calculate your age Example

In this section we will discuss about how to calculate current age without using any pre-define function.

Age calculator


<?php

$users = array (31,28,31,30,31,30,31,31,30,31,30,31);
$cd=date("d");
$cm=date("m");
$cy=date("o");

$dd=28;
$h=12; //Provided Age
$dy=1993;

$dm=12-1;


if($cd < $dd)
{
$cm=$cm-1;
$a=$users[$dm];
$cd=$cd+$a;
$d=$cd-$dd;
}
else
{
$d=$cd-$dd;
}

if($cm < $dm)
{
$cy=$cy-1;
$cm=$cm+12;
$m=$cm-$dm;
}
else
{
$m=$cm-$dm;
}
$y=$cy-$dy;


 echo "year:".$y."
"; echo "Month:".$m."
"; echo "day:".$d."
"; ?>

Output

year:23
Month:5
day:2

Explanation

In this example first store the current day, month, year after that store your date of birth on variable, check conditions and calculate age.


QUOTES OF THE DAY

A single death is tragedy, a million death is a static.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER