How to create the PHP Script to Calculate the Age Using the
Inputs Of our Birth date and the Current date?

Answers were Sorted based on User's Feedback



How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Curre..

Answer / sindhu

<?
$year=$_POST['year'];
$mon=$_POST['mon'];
$dt=$_POST['dt'];
if ($_POST['submit']!="")
{

$a=date("d");
echo "this is cu date".$a."<br>";
$b=date("m");
echo "this is cu month".$b."<br>";
$c=date("Y");
echo "this is cu date".$c."<br>";

if ($b < $mon && $a > $dt )
{
$age=$c-$year-1;
$e=12-$mon;
$month=$b+$e;
$f=$a-$dt;
echo "You
are"."&nbsp;&nbsp;".$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$f."Days"."&nbsp;&nbsp;"."old";
}
if ($b < $mon && $a < $dt )
{
$age=$c-$year-1;
$e=12-$mon-1;
$month=$b+$e;
$f=$dt-$a;
$g=30-$f;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$g."Days";
}
if ($b > $mon && $a > $dt)
{
$age=$c-$year;
$month=$b-$mon;
$f=$a-$dt;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$f."Days";
}
if ($b > $mon && $a < $dt)
{
$age=$c-$year;
$month=$b-$mon-1;
$f=$dt-$a;
$g=30-$f;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$g."Days";
}
if ($b == $mon && $a < $dt )
{
$age=$c-$year-1;
$month=11;
$f=$dt-$a;
$g=30-$f;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$g."Days";
}
if ($b == $mon && $a > $dt )
{
$age=$c-$year;
$month=0;
$f=$a-$dt;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$f."Days";
}
if ($b < $mon && $a == $dt )
{
$age=$c-$year-1;
$e=$mon-$b;
$month=12-$e;
$f=0;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$f."Days";
}
if ($b > $mon && $a == $dt )
{
$age=$c-$year;
$month=$b-$mon;
$f=0;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$f."Days";
}
if ($b == $mon && $a == $dt )
{
$age=$c-$year;
$month=0;
$f=0;
echo
$age."years"."&nbsp;&nbsp;&nbsp;".$month."Months"."&nbsp;&nbsp;".$f."Days";
}
}
?>

Is This Answer Correct ?    6 Yes 3 No

How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Curre..

Answer / prakash.matte

<?php
$dob = '29-05-1986';
$age = date('Y') - date('Y', strtotime($dob));
if (date('md') < date('md', strtotime($dob))) {
$age--;
}
echo $age;
?>

Is This Answer Correct ?    5 Yes 2 No

How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Curre..

Answer / nirav desai

<?php
$a = date('Y',strtotime('1985-10-15'));
$b = date('Y');
$c = $b-$a;
echo "Current age is:: ".$c." Years .";
?>

Is This Answer Correct ?    5 Yes 3 No

How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Curre..

Answer / sindhujob

No

Is This Answer Correct ?    2 Yes 4 No

How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Curre..

Answer / king

<?php

//calculate years of age (input string: YYYY-MM-DD)
function birthday ($birthday){
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($day_diff < 0 || $month_diff < 0)
$year_diff--;
return $year_diff;
}

?>

Is This Answer Correct ?    6 Yes 8 No

How to create the PHP Script to Calculate the Age Using the Inputs Of our Birth date and the Curre..

Answer / sandhya

<?php
$date1 = time();
$date2 = mktime(0,0,0,02,01,1979);
$ddif = $date1 - $date2;
$age = floor(($ddif/(60*60*24))/365);
echo $age;
?>

Is This Answer Correct ?    0 Yes 6 No

Post New Answer

More PHP Interview Questions

i have designed a registration form in html (registration.html) and to insert user details in database i have designed a submit form in php (submit_registration.php) but when i click on submit button in registration.html it opens a dialogue box which asks for open or save php(submit_registration.php) even though i have already installed apache HTTP server and php version 5 on my computer and i am using mysql as backend. plz suggest me what should i do????

3 Answers  


Do you know how can we check the value of a given variable is a number?

0 Answers  


How to pass variables by references?

0 Answers  


Under what circumstance is it impossible to assign a default value to a parameter while declaring a function?

0 Answers  


Explain the syntax for ‘foreach’ loop with example.

0 Answers  






Tell me how a constant is defined in a php script?

0 Answers  


when will be syndicate bank written exam ? i need the date of exam

0 Answers  


Why overriding is called runtime polymorphism?

0 Answers  


What are the differences between GET and POST methods in form submitting?

0 Answers  


Is php a mvc?

0 Answers  


How to copy a file?

0 Answers  


How to redirect a url from http to https in .htaccess?

0 Answers  


Categories