Age calculation from date of birth in PHP scripting language
<!-- Object based, reusable component approach in PHP 5 classes and objects -->
<?php
class DateOfBirth {
private $current_num_year;
private $current_num_month;
private $current_num_day;
private $current_num_month_days;
public $birth_num_year;
public $birth_num_month;
public $birth_num_day;
private $yy;
private $mm;
private $dd;
public $age;
public function DateOfBirth()
{
$this->current_num_year = date(Y);
$this->current_num_month = date(n);
$this->current_num_day = date(j);
$this->current_num_month_days = date(t);
}
private function birthday_before_today()
{
$this->yy = $this->current_num_year - $this->birth_num_year;
$this->mm = $this->current_num_month - $this->birth_num_month - 1;
$this->dd = $this->birth_num_month_days - $this->birth_num_day + $this->current_num_day;
if($this->dd > $this->current_num_month_days)
{
$this->mm += 1;
$this->dd -= $this->current_num_month_days;
}
}
private function birthday_on_today()
{
$this->yy = $this->current_num_year - $this->birth_num_year;
$this->mm = 0;
$this->dd = 0;
}
private function birthday_after_today()
{
$this->yy = $this->current_num_year - $this->birth_num_year - 1;
$this->mm = $this->birth_num_month + $this->current_num_month - 1;
$this->dd = $this->birth_num_month_days - $this->birth_num_day + $this->current_num_day;
if($this->dd > $this->current_num_month_days)
{
$this->mm += 1;
$this->dd -= $this->current_num_day;
}
}
public function calculate_age()
{
$this->birth_num_month_days = date( t, mktime(0, 0, 0, $this->birth_num_month, $this->birth_num_day, $this->birth_num_year) );
if($this->current_num_month > $this->birth_num_month)
{
$this->birthday_before_today();
}
if($this->current_num_month < $this->birth_num_month)
{
$this->birthday_after_today();
}
if($this->current_num_month == $this->birth_num_month)
{
if($this->current_num_day == $this->birth_num_day)
{
$this->birthday_on_today();
}
if($this->current_num_day < $this->birth_num_day)
{
$this->birthday_after_today();
}
if($this->current_num_day > $this->birth_num_day)
{
$this->birthday_before_today();
}
}
$this->age = $this->yy . ' years, ' . $this->mm . ' months, ' . $this->dd . ' days';
}
}
// $my_dob = new DateOfBirth(1981, 6, 23); // If Constructor (function) has arguments
$my_dob = new DateOfBirth();
$my_dob->birth_num_year = 1981;
$my_dob->birth_num_month = 6;
$my_dob->birth_num_day = 23;
$my_dob->calculate_age();
echo $my_dob->age;
?>
<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->
<!-- Object based, reusable component approach in PHP 4 classes and objects ( PHP 5 compatible ) -->
<?php
class DateOfBirth {
var $current_num_year;
var $current_num_month;
var $current_num_day;
var $current_num_month_days;
var $birth_num_year;
var $birth_num_month;
var $birth_num_day;
var $yy;
var $mm;
var $dd;
var $age;
function DateOfBirth()
{
$this->current_num_year = date(Y);
$this->current_num_month = date(n);
$this->current_num_day = date(j);
$this->current_num_month_days = date(t);
}
function birthday_before_today()
{
$this->yy = $this->current_num_year - $this->birth_num_year;
$this->mm = $this->current_num_month - $this->birth_num_month - 1;
$this->dd = $this->birth_num_month_days - $this->birth_num_day + $this->current_num_day;
if($this->dd > $this->current_num_month_days)
{
$this->mm += 1;
$this->dd -= $this->current_num_month_days;
}
}
function birthday_on_today()
{
$this->yy = $this->current_num_year - $this->birth_num_year;
$this->mm = 0;
$this->dd = 0;
}
function birthday_after_today()
{
$this->yy = $this->current_num_year - $this->birth_num_year - 1;
$this->mm = $this->birth_num_month + $this->current_num_month - 1;
$this->dd = $this->birth_num_month_days - $this->birth_num_day + $this->current_num_day;
if($this->dd > $this->current_num_month_days)
{
$this->mm += 1;
$this->dd -= $this->current_num_day;
}
}
function calculate_age()
{
$this->birth_num_month_days = date( t, mktime(0, 0, 0, $this->birth_num_month, $this->birth_num_day, $this->birth_num_year) );
if($this->current_num_month > $this->birth_num_month)
{
$this->birthday_before_today();
}
if($this->current_num_month < $this->birth_num_month)
{
$this->birthday_after_today();
}
if($this->current_num_month == $this->birth_num_month)
{
if($this->current_num_day == $this->birth_num_day)
{
$this->birthday_on_today();
}
if($this->current_num_day < $this->birth_num_day)
{
$this->birthday_after_today();
}
if($this->current_num_day > $this->birth_num_day)
{
$this->birthday_before_today();
}
}
$this->age = $this->yy . ' years, ' . $this->mm . ' months, ' . $this->dd . ' days';
}
}
// $my_dob = new DateOfBirth(1981, 6, 23); // If Constructor (function) has arguments
$my_dob = new DateOfBirth();
$my_dob->birth_num_year = 1981;
$my_dob->birth_num_month = 6;
$my_dob->birth_num_day = 23;
$my_dob->calculate_age();
echo $my_dob->age;
?>
<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->
<!-- Functional, modular or structural approach in PHP -->
<?php
function calculate_age($birth_num_year, $birth_num_month, $birth_num_day)
{
$birth_num_month_days = date( t, mktime(0, 0, 0, $birth_num_month, $birth_num_day, $birth_num_year) );
$current_num_year = date(Y);
$current_num_month = date(n);
$current_num_day = date(j);
$current_num_month_days = date(t);
if($current_num_month > $birth_num_month)
{
$yy = $current_num_year - $birth_num_year;
$mm = $current_num_month - $birth_num_month - 1;
$dd = $birth_num_month_days - $birth_num_day + $current_num_day;
if($dd > $current_num_month_days)
{
$mm += 1;
$dd -= $current_num_month_days;
}
}
if($current_num_month < $birth_num_month)
{
$yy = $current_num_year - $birth_num_year - 1;
$mm = $birth_num_month + $current_num_month - 1;
$dd = $birth_num_month_days - $birth_num_day + $current_num_day;
if($dd > $current_num_month_days)
{
$mm += 1;
$dd -= $current_num_day;
}
}
if($current_num_month==$birth_num_month)
{
if($current_num_day == $birth_num_day)
{
$yy = $current_num_year - $birth_num_year;
$mm = 0;
$dd = 0;
}
if($current_num_day < $birth_num_day)
{
$yy = $current_num_year - $birth_num_year - 1;
$mm = $birth_num_month + $current_num_month - 1;
$dd = $birth_num_month_days - $birth_num_day + $current_num_day;
if($dd > $current_num_month_days)
{
$mm += 1;
$dd -= $current_num_day;
}
}
if($current_num_day > $birth_num_day)
{
$yy = $current_num_year - $birth_num_year;
$mm = $current_num_month - 1;
$dd = $birth_num_month_days - $birth_num_day + $current_num_day;
if($dd > $current_num_month_days)
{
$mm += 1;
$mm -= $current_num_month;
$dd -= $current_num_month_days;
}
}
}
$age = $yy . ' years, ' . $mm . ' months, ' . $dd . ' days';
echo $age;
}
calculate_age(1981, 6, 23);
?>
<!-- mediasworks.org -->
<!-- mediasworks Group, India and worldwide -->
<!-- mediasworks nonprofit public participation project -->