Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

age in months 1

Status
Not open for further replies.

prakash2000

Technical User
Oct 23, 2009
8
0
0
IN
dear friends,
i have inclusion criteria for age group between 9 and 11.99 in months.how to i check dob in the above range in php.

im new in php.

thanks in advance
prakash
 
pre php 5.3 there is no easy way to handle time intervals in anything other than seconds. so we do things programmatically

Code:
<?php
echo (validates('2009-01-01') === true) ? 'ok': 'not ok';

function validates($dob){
	if (!is_numeric($dob)) $dob = strtotime($dob);//check if already in unix timestamp format
	$now = time();
	if (strtotime('+1 year', $dob) <= $now) return false; //check whether more than 11.99 months
	if (strtotime('+9 months', $dob) > $now) return false ;//check whether less than 9 months have passed
	return true;
}
?>

if you have php 5.3 then the new datetime class will work well

Code:
function validates($dob){
	$dob = new DateTime($dob);
	$diff = $dob->diff(new datetime());
	$months = abs($diff->months);
	if ($months > 12 || $months < 9) return false;
	return true;
}

if you have a database such as mysql then the code is easier still

Code:
function validates($dob){
	return (bool) mysql_result(mysql_query("select if (date_add('$dob', interval 9 month) <= now() && date_add('$dob', interval 12 month) > now()), true, false)"), 0, 0);
}
 
i should add that it is good practice to ensure that a valid time zone is set before using any of php's date/time functions
 
Thank you very much jpadie. the script is perfect
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top