Dangit I thought I could do THIS simple task today and even this is beyond me!!
<?= date(time()) - date(strtotime($result[0]->birth)) ?>
This is completely wrong, but what IS right? I'm stumped!
Thanx
Phil
Dangit I thought I could do THIS simple task today and even this is beyond me!!
<?= date(time()) - date(strtotime($result[0]->birth)) ?>
This is completely wrong, but what IS right? I'm stumped!
Thanx
Phil
There must be lots of ways to do this...
$years = ( time() - strtotime( $result[0]->birth )) / 31556926; // Hmm leap years...
$years2 = date( 'Y' ) - date( 'Y', strtotime( $result[0]->birth )); // Leap years ok?
date( 'n', strtotime( $result[0]->birth )) > date( 'n' ) and --$years2; // Check if birthday has passed
This is what I came up with based on your code:
if (!function_exists('get_age')) {
/**
* Determine age based upon date of birth. Implicit 'n/j/Y' (no-leading-zero month/day with full year) unless specified
*
* @access public
* @param date $birthDate
* @param mixed $format (default = 'n/j/Y')
* @return int $age
*/
function &get_age($birthDate, $format = 'n/j/Y') {
$years = (time() - strtotime($format, $birthDate)) / 31556926; // Hmm leap years...
$years2 = date('Y') - date('Y', strtotime($format, $birthDate)); // Leap years ok?
return date('n', strtotime($format, $birthDate)) > date('n') and --$years2; // Check if birthday has passed
}
}
Sorry, but this always returns 1, regardless of date of birth and/or time of day.
Phil
Upon trying this:
if (!function_exists('get_age')) {
/**
* Determine age based upon date of birth. Implicit 'n/j/Y' (no-leading-zero month/day with full year) unless specified
*
* @access public
* @param int $birth_day
* @param int $birth_month
* @param int $birth_year
* @return int $age
*/
function &get_age($birth_day, $birth_month, $birth_year) {
$datestamp = date("d.m.Y", mktime());
$t_arr = explode("." , $datestamp);
$current_day = $t_arr[0];
$current_month = $t_arr[1];
$current_year = $t_arr[2];
$year_dif = $current_year - $birth_year;
if(($birth_month > $current_month) || ($birth_month == $current_month && $current_day < $birth_day))
$age = $year_dif - 1;
else
$age = $year_dif;
return $age;
}
}
list($birth_month, $birth_day, $birth_year) = @explode('/', date('n/j/Y', strtotime($result[0]->birth)));
echo get_age($birth_month, $birth_day, $birth_year); // RETURNS 0 NO MATTER WHAT THE AGE OR TIME
It's not 1 this time, it's 0!
Phil
Is this your actual code?
Isn't this function expecting to have parameters passed in? Like:
$mm = 5;
$dd = 20;
$ccyy = 1960;
$age = get_age($mm,$dd,$ccyy);
This works for me:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Age</title>
</head>
<body>
<?php
function &get_age($birth_day, $birth_month, $birth_year) {
$datestamp = date("d.m.Y", mktime());
$t_arr = explode("." , $datestamp);
$current_day = $t_arr[0];
$current_month = $t_arr[1];
$current_year = $t_arr[2];
$year_dif = $current_year - $birth_year;
if(($birth_month > $current_month) || ($birth_month == $current_month && $current_day < $birth_day)) {
$age = $year_dif - 1;
} else {
$age = $year_dif;
}
return $age;
}
$mm = 5;
$dd = 20;
$ccyy = 1960;
$age = get_age($mm,$dd,$ccyy);
echo $mm."/".$dd."/".$ccyy."<br />age: ".$age;
?>
</body>
</html>
I get age this output:
5/20/1960
age: 45
This works for me:
for ($i = 0; $i < 20; $i++) {
$day = mt_rand(1, 28);
$month = mt_rand(1, 12);
$year = mt_rand(1901, 2005);
echo calculate_age($day, $month, $year) . ', ';
}
function calculate_age($birth_day, $birth_month, $birth_year) {
$datestamp = date("d.m.Y", mktime());
$t_arr = explode("." , $datestamp);
$current_day = $t_arr[0];
$current_month = $t_arr[1];
$current_year = $t_arr[2];
$year_dif = $current_year - $birth_year;
if(($birth_month > $current_month) || ($birth_month == $current_month && $current_day < $birth_day))
$age = $year_dif - 1;
else
$age = $year_dif;
return $age;
}
echos this:
55, 22, 80, 70, 90, 84, 68, 35, 47, 92, 3, 77, 11, 79, 79, 66, 83, 22, 33, 65,
Then I'm at a complete loss, I don't know what to do, why does mine fail?
Phil
What type do you have the dob stored as?
Try taking the function out of the if statement. Also, echo the string you're exploding to see what that value looks like.
Installer wrote:What type do you have the dob stored as?
MySQL 4.1.13 MyISAM table type = datatype datetime
Phil
adavis wrote:Try taking the function out of the if statement.
That makes no sense. I did that and it still fails, but then again it should fail the same way,
if (!function_exists('get_age')) {
function &get_age(..) {
}
}
Phil
Try this:
list($date) = explode(' ', $datetime); // $datetime = db datetime result
list($year, $month, $day) = explode('-', $date);
echo get_age($month, $day, $year);
That produced "2006".
Phil
I don't understand why you have the function inside and if statement. Personally, I've never tried that, but, then I'm no expert. I try to stay as basic as possible and not get to fancy. The example that I tried doesn't have function inside an if statement, but I'm sure that your code has a lot more to it that my simple example and that you must have a reason for putting it inside an if statement.
I think what Installer was asking was about what type do you have $result[0]->birth store as. Because you are exploding on "/", I would think it's a string, something like "12/20/83". However, you might want to echo that to see what the value actually is. I find that if I do that, sometimes what I think I have stored isn't what I have at all.
How about this:
echo $result[0]->birth;
GOT IT!!!!!!!!!!
[PHP}
<?
list($birth_year, $birth_month, $birth_day) = @explode('-', $result[0]->birth);
echo get_age($birth_month, $birth_day, $birth_year);
?>
[/code]
Phil
ppowell wrote:Sorry, but this always returns 1, regardless of date of birth and/or time of day.
That's not really surprising given your code. First of all strtotime() doesn't accept any formatting parameters as per date, it just creates a timestamp based on the input. Secondly you were returning 'date( ... ) and --$years' which presumably evaluates to true based on your input. Fixed code below.
function &get_age($birthDate) {
$years = date('Y') - date('Y', strtotime($birthDate));
date('z', strtotime($birthDate)) > date('n') and --$years;
return $years;
}
echo get_age( '1975-04-25' ); // 30
Glad it's sorted either way though
According to that, I'm 2006 years old. Not far off.
Installer wrote:According to that, I'm 2006 years old. Not far off.
How on earth are you getting 2006? You did see the date format should be ISO standard YYYY-MM-DD right? I've noticed a typo in line 2 though - date( 'n' ) should be date( 'z' ) to determine whether birth day has passed yet. Still shouldn't have been more than a year out with the typo.
for ($i = 0; $i < 20; $i++) {
$day = mt_rand(1, 28);
$month = mt_rand(1, 12);
$year = mt_rand(1901, 2005);
echo get_age( "$year-$month-$day" ) . "($year-$month-$day), ";
}
function &get_age($birthDate) {
$years2 = date('Y') - date('Y', strtotime($birthDate));
date('z', strtotime($birthDate)) > date('z') and --$years2;
return $years2;
}
and the results:
45(1961-1-6), 51(1955-1-27), 41(1964-5-7), 45(1960-3-13), 17(1988-5-3), 13(1992-6-22), 82(1923-3-6), 21(1984-8-2), 10(1995-9-6), 30(1975-7-11), 51(1954-9-7), 45(1960-2-25), 69(1936-2-27), 84(1921-11-20), 19(1986-8-26), 69(1936-11-20), 8(1997-7-17), 85(1921-1-24), 85(1920-11-11), 27(1978-5-15),