Calculators
|
|
|
|
<?php function days_in_month($month, $year) {
$lastday = mktime (0,0,0,($month+1),0,$year);
$days = strftime("%d", $lastday);
return $days;
} ?>
<?php function age_calculate($date_from, $date_to) {
define("A_DAY", 86400); // One day in seconds
define("A_WEEK", 604800); // One week in seconds
$age['years'] = $date_to['year'] - $date_from['year'];
$age['monthsTotal'] = ( $date_to['mon'] + (12*$date_to['year']) ) - ( $date_from['mon'] + (12*$date_from['year']) );
// month and monthday is smaller than birth
if ( ($date_to['mon'] <= $date_from['mon']) && ($date_to['mday'] <= $date_from['mday']) ) {
$age['years'] -= 1;
$age['monthsTotal'] -= 1;
}
$age['months'] = $age['monthsTotal'] - ( $age['years'] * 12 );
$age['daysExcact'] = ($date_to[0] - $date_from[0]) / A_DAY;
$age['days'] = floor( ($date_to[0] - $date_from[0]) / A_DAY );
$age['weeksExcact'] = ($date_to[0] - $date_from[0]) / A_WEEK;
$age['weeks'] = floor( ($date_to[0] - $date_from[0]) / A_WEEK );
if ( $date_to['mday'] < $date_from['mday'] ) {
$age['modMonthDays'] = days_in_month($date_to['mon']-1,$date_to['year']) - ($date_from['mday'] - $date_to['mday']);
} else {
$age['modMonthDays'] = $date_to['mday'] - $date_from['mday'];
}
return $age;
} ?>
|
|
|
Usage Example
|
<?php
$date_birth = mktime(0,0,0,3,13,2001); $date_today = mktime();
$today = getdate($date_today); $birth = getdate($date_birth);
$age = age_calculate($birth, $today); $birthday = FALSE;
// Write content of array foreach ($age as $key=>$val) {
echo "$key: $val<br/>";
}
// Presentation if ( $today['mon'] == 3 && $today['mday'] == 13 ) {
$write_age = "<b>" . $age['years'] . " years</b> - CONGRATULATIONS!!!";
$birthday = TRUE;
} else {
if ($age['months'] != 0 ) {
$write_mon = $age['months'] . " month(s) ";
}
if ($age['modMonthDays'] != 0 ) {
$write_days = " og " . $age['modMonthDays'] . " day(s)";
}
$write_age = $age['years'] . " years " . $write_mon . $write_days;
}
echo "$write_age"; ?>
|
|
|
Rate This Script
|
|
|
|