Skip to main content

Calculate Age

This class can calculate a person's age and also display values like the number of days and weeks lived.

<?php
class Age
{
var $age = '';

function calculateAge($iTimestamp)
{
$iDiffYear = date('Y') - date('Y', $iTimestamp);
$iDiffMonth = date('n') - date('n', $iTimestamp);
$iDiffDay = date('j') - date('j', $iTimestamp);

// If birthday has not happen yet for this year, subtract 1.
if ($iDiffMonth < 0 || ($iDiffMonth == 0 && $iDiffDay < 0))
{
$iDiffYear--;
}

$this->age = $iDiffYear;
}

function getAge()
{
return $this->age;
}

function get_rank($rank)
{
$last = substr( $rank, -1 );
$seclast = substr( $rank, -2, -1 );
if( $last > 3 || $last == 0 ) $ext = 'th';
else if( $last == 3 ) $ext = 'rd';
else if( $last == 2 ) $ext = 'nd';
else $ext = 'st';

if( $last == 1 && $seclast == 1) $ext = 'th';
if( $last == 2 && $seclast == 1) $ext = 'th';
if( $last == 3 && $seclast == 1) $ext = 'th';

return $rank.$ext;
}

}

$dob = '1985-06-18';
$dob2 = explode("-",$dob);

$dob_hour = 18; // 24 hour format
$dob_min = 41;
$dob_sec = 0;

$d = getdate(); // Current date

$year=$d['year'];
$mon=$d['mon'];
$mday=$d['mday'];

$hour = $d['hours'];
$min = $d['minutes'];
$sec = $d['seconds'];


$d1=mktime($dob_hour,$dob_min,$dob_sec,$dob2[1],$dob2[2],$dob2[0]);
$d2=mktime($hour,$min,$sec,$mon,$mday,$year);

$obj = new Age;

$obj->calculateAge(mktime($dob_hour,$dob_min,$dob_sec,6,18,1985));

$age = $obj->getAge();
$rank = $obj->get_rank($age+1);

echo 'Your age: '.$age;
echo '<br>';
echo 'You are running: '.$rank.' year';

echo '<br><br>';
echo '<b>You already lived:</b> <br><br>';

echo "Years: ".floor(($d2-$d1)/31536000) . "<br>";
echo "Months: ".floor(($d2-$d1)/2628000) . "<br>";
echo "Weeks: ".floor(($d2-$d1)/604800) . "<br>";
echo "Days: ".floor(($d2-$d1)/86400) . "<br><br>";

echo "Hours: ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes: ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds: " .($d2-$d1). "<br><br>";
?>

Output: (click below image to enlarge)

Comments

Popular posts from this blog

php strip all tags

The below function can strip almost all tags from a string. function strip_all_tags($string) {     $string = preg_replace( '@<(script|style)[^>]*?>.*?@si', '', $string );     $string = strip_tags($string);     return trim($string); } $a = '<script type="text/javascript" src="jquery.js"></script> <div id="test" style="padding:5px; color:red;">Hello world</div>'; echo strip_all_tags($a); // outputs: Hello world

htaccess disable directory browsing, disable file listing, disable file access

There may be some personal or secure data in a file, that you want to hide from the end user, when it is viewed through a web browser. Ex: http://www.example.com/user_files/24/chat.txt In the above scenario, if the file "chat.txt" contains a secure data, and you dont want to allow end users to directly access the file, or to disable the file listing in the folder "user_files" or "24", use the below code in your .htaccess file inside "user_files" folder: Options -Indexes order allow,deny deny from all

dropdown menu over flash banner

It is often annoying in some browsers when the dropdown menu at the top of the flash banner is displayed at the back of the flash. It can be corrected with some minor changes in the flash embed code: 1. for embed tag, add wmode="transparent" 2. add param to the object tag: <param name="wmode" value="transparent" />