That produced "2006".
Phil
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),
Yes, I noticed and followed the format. What I didn't mention, and should have (sorry), was that this was on a Windows machine with PHP < 5.1.0, upon which, of course, the function won't work even without the typo.
Do any sane people actually run PHP in a production environment on Windows? :p
Shrike wrote:Do any sane people actually run PHP in a production environment on Windows? :p
Those given no choice but to do so. I had a requirement to build a portable PHP web application that had to work in both Windows and Linux (there were some security bubbles in Windows not to mention some slight functionality loss)
Phil
To come at this from the (MySQL) database, as of version 4.1 MySQL has a DATEDIFF() function to calculate number of days between two dates. As far as I know MSSQL has a similar function.
hello
I know this is a old thread
but
I found the code useful
I created a form to insert the data
<html>
<head><title>Age with field</title>
</head>
<body>
<form action="doage.php" method="post">
Enter birthday (ex.12/07/1978)<br><br>
Month:<input type="text" size="2" name="mm">Day:<input type="text" size="2" name="dd">
Year:<input type="text" size="4" name="ccyy"><br><br>
<input type="submit">
</form>
</body>
the code works but their is no validation
for example 12/33/1234 age 771
<!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 = $POST['mm'];
$dd = $POST['dd'];
$ccyy = $_POST['ccyy'];
$age = get_age($mm,$dd,$ccyy);
echo $mm."/".$dd."/".$ccyy."<br />age: ".$age;
?>
</body>
</html>