How can we calculate age in PHP?

Wednesday, October 14, 2009 14:06

Simple Age Calculation in PHP:

<?php

		// make sure to set timezone //
		date_default_timezone_set('Europe/London');

		function getAge( $dob, $tdate ) // int $dob, int $tdate, int years
		{
			$age = 0;
			while( $tdate > $dob = strtotime('+1 year', $dob)){
			 ++$age;
			}
			return $age;
		}

		### Date of Birth ###
		$dob = strtotime("april 20 1966");

		### Current Date ***/
		$tdate = strtotime("june 2 2009");

		### show the date ***/
		echo getAge( $dob, $tdate );

?>
  • Share/Bookmark
You can leave a response, or trackback from your own site.

Leave a Reply