note 86086 added to function.mktime

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Here is what I use to calculate age. It took me 30 minutes to write and it's quite accurate. What it has special is that it's calculating the number of days a year has (float number), by testing if a year is a leap one or not. This number is used to compute the age.

function get_age($date_start, $date_end) {
	$t_lived = get_timestamp($date_end) - get_timestamp($date_start);
	$seconds_one_year = get_days_per_year($date_start, $date_end) * 24 * 60 * 60;
	$age = array();
	$age['years_exact'] = $t_lived / $seconds_one_year;
	$age['years'] = floor($t_lived / $seconds_one_year);
	$seconds_remaining = $t_lived % $seconds_one_year;
	$age['days'] = round($seconds_remaining / (24 * 60 * 60));
	return $age;
}
function get_timestamp($date) {
	list($y, $m, $d) = explode('-', $date);
	return mktime(0, 0, 0, $m, $d, $y);
}
function get_days_per_year($date_start, $date_end) {
	list($y1) = explode('-', $date_start);
	list($y2) = explode('-', $date_end);
	$years_days = array();
	for($y = $y1; $y <= $y2; $y++) {
		$years_days[] = date('L', mktime(0, 0, 0, 1, 1, $y)) ? 366 : 365;
	}
	return round(array_sum($years_days) / count($years_days), 2);
}

$date_birth = '1979-10-12';
$date_now = date('Y-m-d');

$age = get_age($date_birth, $date_now);
echo '<pre>';
print_r($age);
echo '</pre>';

It will display something like this:
Array
(
    [years_exact] => 28.972974329491
    [years] => 28
    [days] => 355
)
----
Server IP: 194.102.65.226
Probable Submitter: 86.121.233.11
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit        -- https://master.php.net/note/edit/86086
Del: integrated  -- https://master.php.net/note/delete/86086/integrated
Del: useless     -- https://master.php.net/note/delete/86086/useless
Del: bad code    -- https://master.php.net/note/delete/86086/bad+code
Del: spam        -- https://master.php.net/note/delete/86086/spam
Del: non-english -- https://master.php.net/note/delete/86086/non-english
Del: in docs     -- https://master.php.net/note/delete/86086/in+docs
Del: other reasons-- https://master.php.net/note/delete/86086
Reject      -- https://master.php.net/note/reject/86086
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.