Age Calculator

Hey there, I’m trying to generate a birthdate of a person
into seconds, minutes, days and years.
this is what I have done


<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 //EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
 <head>
  <title>Date () in PHP</title>
 </head>
 <body>
	<?php
	echo '<p> Tell you a secret. My birthday is '. date( 'F jS, Y' , mktime(0,0,0,7,22,1991) ) ."</p>";
	echo '<p> Today is: '. date('Y-m-d') ."</p>"; 


	//birth date in terms of seconds since the epoch 
	$birthtime=mktime(0, 0, 0, 22, 7, 1991); 
	//now in terms of seconds since the epoch 
	$now=time(); 

	$age=$now-$birthtime;  //age in seconds
	$age=$age/60;  //age in minutes 
	$age=$age/60;  //age in hours 
	$age=$age/24;  //age in days 
	$age=$age/356; //age in years
	 
	
	echo 'You are ' . floor($age) . ' .';
	
	?>
 </body>
</html>

By looking at the current date : Feb 21 2011, I should be getting
7151 days since birth
171624 hours since birth
10297440 minutes since birth
not sure about the seconds

but I think my code is not generating the correct figures.

one more problem, since $age is related to each other, I only get the overall figure, but I want to generate one by one.

Why? What figures do you get?

one more problem, since $age is related to each other, I only get the overall figure, but I want to generate one by one.

Don’t use the same variable ($age) for each calculation. Or display the value before doing the next calculation.

should it be like this


	$birthtime=mktime(0, 0, 0, 22, 7, 1991); 
	//now in terms of seconds since the epoch 
	$now=time(); 

	$age=$now-$birthtime;  //age in seconds
	
	$age1=$now-$birthtime
	$age1=$age1/60;  //age in minutes 
	
	$age2=$now-$birthtime
	$age2=$age2/60;  //age in hours 
	
	$age3=$now-$birthtime
	$age3=$age3/24;  //age in days 
	
	$age4=$now-$birthtime
	$age4=$age4/356; //age in years
	 
	
	echo 'You are ' . floor($age) . ' seconds.';
	echo 'You are ' . floor($age1) . ' minutes.';
	echo 'You are ' . floor($age2) . ' hours.';
	echo 'You are ' . floor($age3) . ' days.';
	echo 'You are ' . floor($age4) . ' years.';
	


             $age = $now - $birthtime;  //age in seconds
	
	$age1 = $age / 60;  //age in minutes 
	
	$age2 = $age1 / 60;  //age in hours 
	
	$age3 = $age2 / 24;  //age in days 
	
	$age4 = $age3 / 365; //age in years
	
	echo 'You are ' . floor($age) . ' seconds.';
	echo 'You are ' . floor($age1) . ' minutes.';
	echo 'You are ' . floor($age2) . ' hours.';
	echo 'You are ' . floor($age3) . ' days.';
	echo 'You are ' . floor($age4) . ' years.';

A normal year has 365 days, not 356 :slight_smile:

Thanks a lot!

Hi,
You can use this code for calculating the age

$ageTime = mktime(0, 0, 0, 9, 9, 1919); // Get the person’s birthday timestamp
$t = time(); // Store current time for consistency
$age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;//age
$year =$age60 60 * 60 * 24 * 365;
$ageYears = $age / $year;
$age0=$year/60; //age in seconds
$age1=$age0/60; //age in minutes
$age2=$age1/60; //age in hours
$age3=$age2/24; //age in days
$age4=$age3/365; //age in days

echo ‘You are ’ . floor($age0) . ’ seconds.’;
echo ‘You are ’ . floor($age1) . ’ minutes.’;
echo ‘You are ’ . floor($age2) . ’ hours.’;
echo ‘You are ’ . floor($age3) . ’ days.’;
echo ‘You are ’ . floor($age4) . ’ years.’;