Location: PHPKode > scripts > Validate-Calculate Age > validate-calculate-age/vcage.class.php
<?php
/*
Validate-Calculate Age Class by FergoFrog
Copyright (C) 2009 FergoFrog

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
1307, USA.

Contact:
Email: hide@address.com
Web: http://www.fergofrog.com/ or http://projects.fergofrog.com/
*/
	/* 	Class VCAge
		See usage.php for usage
		See example.php for an example of how to use this class
		See comments above functions for more information regarding the function
	*/
	class VCAge
	{
		public $birth_day;
		public $birth_month;
		public $birth_year;
		public $Error = 'Test';
		/* 	Validate Year
			- Checks for Integer
			- Checks integer is between One and the Current Year
		*/
		private function VYear($birth_year)
		{
			if (is_int($birth_year))
			{
				if ($birth_year>=1&&$birth_year<=intval(date("Y"))) return true;
				else return false;
			}
		}
		/* 	Validate Month
			- Checks for Integer
			- Checks integer is between One and the Twelve
		*/
		private function VMonth($birth_month)
		{
			if (is_int($birth_month))
			{
				if ($birth_month>=1&&$birth_month<=12) return true;
				else return false;
			}
			else return false;
		}
		/* 	Validate Day
			- Checks for Integer
			- Checks for Leap Year
			- Checks for Days in Month (for Feb. it modifies according to Leap Year)
			- Checks integer is between One and the Maximum Day of the Month
		*/
		private function VDay($birth_day, $birth_month, $birth_year)
		{
			if (is_int($birth_day))
			{
				if ($birth_month==1||$birth_month==3||$birth_month==5||$birth_month==7||$birth_month==8||$birth_month==10||$birth_month==12)
				{
					if ($birth_day>=1&&$birth_day<=31) return true;
					else return false;
				}
				elseif ($birth_month==2)
				{
					if ($birth_year % 4 == 0 && ($birth_year % 100 != 0 || $birth_year % 400 == 0)) $febdate = 29;
					else $febdate = 28;
					if ($birth_day>=1&&$birth_day<=$febdate) return true;
					else return false;
				}
				elseif ($birth_month==4||$birth_month==6||$birth_month==9||$birth_month==11)
				{
					if ($birth_day>=1&&$birth_day<=30) return true;
					else return false;
				}
				else
				{
					return false;
				}
			}
			else return false;			
		}
		/* 	Validate Age
			- Checks for Valid Year, then Month, then Day
			- Returns True on success and sets birth_* variables according to their respective valid integers
			- Returns False on failure and sets Error variable according to break point
		*/
		public function VAge($birth_day, $birth_month, $birth_year)
		{
			if ($this->VYear(intval($birth_year)))
			{
				if ($this->VMonth(intval($birth_month)))
				{
					if ($this->VDay(intval($birth_day), intval($birth_month), intval($birth_year)))
					{
						return true;
						$this->birth_day = $birth_day;
						$this->birth_month = $birth_month;
						$this->birth_year = $birth_year;
					}
					else
					{
						$this->Error = 'Date Invalid';
						return false;
					}
				}
				else
				{
					$this->Error = 'Month Invalid';
					return false;
				}
			}
			else
			{
				$this->Error = 'Year Invalid';
				return false;
			}
		}
		/* 	Calculate Age
			- Finds difference between the current and the set year, month and day
			- If the month_difference is less than Zero the year_difference is decreased
			- If the month_difference is Zero and the day_difference is less than Zero the year_difference is decreased
			- Returns the year_difference as age
		*/
		public function CAge($birth_day, $birth_month, $birth_year)
		{
			$year_difference = date("Y") - intval($birth_year);
			$month_difference = date("m") - intval($birth_month);
			$day_difference = date("d") - intval($birth_day);
			if ($month_difference < 0) $year_difference--;
			elseif ($month_difference==0&&$day_difference<0) $year_difference--;
			$age = $year_difference;
			return $age;
		}
		/* 	Validate Year
			- Checks for Valid Age
			- If Valid Age executes Calculate Age function
			- If Invalid Age returns the error message
		*/
		public function V_CAge($birth_day, $birth_month, $birth_year)
		{
			if ($this->VAge($birth_day, $birth_month, $birth_year))
			{
				return $this->CAge($birth_day, $birth_month, $birth_year);
			}
			else
			{
				return 'Invalid Age: ' . $this->Error;
			}
		}
	}
?>
Return current item: Validate-Calculate Age