booneball
asked on
PHP Calculate Age
For some reason when I run the code below it gives me a blank page ie nothing loads. Any ideas???
function birthday ($raw_bdates)
{
list($year,$month,$day) = explode("-",$raw_bdates);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday("1980-07-02");
}
function birthday ($raw_bdates)
{
list($year,$month,$day) = explode("-",$raw_bdates);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday("1980-07-02");
}
Hello,
there is a coding error:
Should be:
echo birthday("1980-07-02");
instead of:
echo birthday("1980-07-02");
}
Regards,
Chorch
there is a coding error:
Should be:
echo birthday("1980-07-02");
instead of:
echo birthday("1980-07-02");
}
Regards,
Chorch
ASKER
Thankyou thats worked. The idea of this function is that it gets the dob from the database (which it does fine)
$raw_bdates =($row['dob']);
it then does the calculate age function
function birthday ($raw_bdates)
{
list($year,$month,$day) = explode("-",$raw_bdates);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday("1988-07-02");
and should then display the age depending on what the dob is in the database.
At the moment it is given me an age of 23 which is correct because the date is in the echo part. However how would i get rid of this and get it to print out what the result of the function was on the date in the database.
$raw_bdates =($row['dob']);
it then does the calculate age function
function birthday ($raw_bdates)
{
list($year,$month,$day) = explode("-",$raw_bdates);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday("1988-07-02");
and should then display the age depending on what the dob is in the database.
At the moment it is given me an age of 23 which is correct because the date is in the echo part. However how would i get rid of this and get it to print out what the result of the function was on the date in the database.
hello,
this code should work after the function:
// echo birthday("1988-07-02");
$result = mysql_query("SELECT dob FROM table WHERE `something`='asdf'");
$row = mysql_fetch_array($result) ;
echo birthday($row['dob']);
where "table" is the table that contains the dob, and you have to set the WHERE condition.
Regards
this code should work after the function:
// echo birthday("1988-07-02");
$result = mysql_query("SELECT dob FROM table WHERE `something`='asdf'");
$row = mysql_fetch_array($result)
echo birthday($row['dob']);
where "table" is the table that contains the dob, and you have to set the WHERE condition.
Regards
Hi Booneball,
load the birthdate as the variable in the function:
load the birthdate as the variable in the function:
$raw_bdates =($row['dob']);
function birthday ($raw_bdates)
{
list($year,$month,$day) = explode("-",$raw_bdates);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday($raw_bdates);
ASKER
if i do that it gives me the year not the age
How is dob saved in the database?
is it in string format ("1988-07-02") ?
or as a timestamp?
to be sure, run this:
$raw_bdates =($row['dob']);
echo $raw_bdates;
is it in string format ("1988-07-02") ?
or as a timestamp?
to be sure, run this:
$raw_bdates =($row['dob']);
echo $raw_bdates;
what does the mysql query return?
echo $row['dob'];
echo $row['dob'];
ASKER
ok so if i echo the dob out then i get:
2011
Happy Birthday to James Lowley!Mail Sent.2011-11-07
dob is a timestamp
2011
Happy Birthday to James Lowley!Mail Sent.2011-11-07
dob is a timestamp
See practical application #1 on this page:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
The reason for the blank page is probably a PHP parse error. Add these two instructions to the top of your script.
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
The column definition for dob should be DATE or DATETIME.
Please let us know if any of this does not make sense, thanks. ~Ray
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
The reason for the blank page is probably a PHP parse error. Add these two instructions to the top of your script.
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
The column definition for dob should be DATE or DATETIME.
Please let us know if any of this does not make sense, thanks. ~Ray
The you should convert timestamp to date format:
$dob = date('Y-m-d', $row['dob']);
echo birthday($dob);
Regards
$dob = date('Y-m-d', $row['dob']);
echo birthday($dob);
Regards
ASKER
Makes no difference still returns the year (unless im being incredibly stupid and not noticing something in the code below)
function birthday ()
{
list($year,$month,$day) = explode("-");
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
$dob = date('Y-m-d',$row['dob']);
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday($dob);
function birthday ()
{
list($year,$month,$day) = explode("-");
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
$dob = date('Y-m-d',$row['dob']);
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday($dob);
ASKER
the whole code
<?PHP
include_once("../info_db/c onn.php");
echo "test1";
function birthday ()
{
list($year,$month,$day) = explode("-");
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
$dob = date('Y-m-d',$row['dob']);
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday($dob);
echo "test2";
//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result) ;
echo "test3";
//Going through all the bdates
while($row = mysql_fetch_array($sql)){
// Run the function to grab the month and day only
$raw_bdates =($row['dob']);
$bdates_month = substr ($raw_bdates, 8, 2);
$bdates_year = substr ($raw_bdates, 2, 2);
$bdates = $bdates_year.$bdates_month ;
$name=($row['name']);//you r name field supposed as person_name
echo "test4";
// See what today is
$now = date("Ymd");
// Only grab the month and day from today
$now = substr($now, 4, 7);
//echo $bdates;
// Seeing if there is a match then email them a birthday message
//echo $now, $bdates."<br>";
if($now == $bdates) {
echo "Happy Birthday to $name!";
//echo $now;
$email = $row['email'] .","; // to be checked
//echo $email;
echo "test5";
// Preparing for email
$subject = "Happy Birthday from Hayward Baker";
$message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
$header = "From:" . "James.Lowley@hayward-bake r.com";
//echo $name;
mail ($email, $subject, $message, $header);
echo "Mail Sent.";
//echo $row['dob'];
}
echo "test6";
}
?>
Please ignore the tests
<?PHP
include_once("../info_db/c
echo "test1";
function birthday ()
{
list($year,$month,$day) = explode("-");
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
$dob = date('Y-m-d',$row['dob']);
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday($dob);
echo "test2";
//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result)
echo "test3";
//Going through all the bdates
while($row = mysql_fetch_array($sql)){
// Run the function to grab the month and day only
$raw_bdates =($row['dob']);
$bdates_month = substr ($raw_bdates, 8, 2);
$bdates_year = substr ($raw_bdates, 2, 2);
$bdates = $bdates_year.$bdates_month
$name=($row['name']);//you
echo "test4";
// See what today is
$now = date("Ymd");
// Only grab the month and day from today
$now = substr($now, 4, 7);
//echo $bdates;
// Seeing if there is a match then email them a birthday message
//echo $now, $bdates."<br>";
if($now == $bdates) {
echo "Happy Birthday to $name!";
//echo $now;
$email = $row['email'] .","; // to be checked
//echo $email;
echo "test5";
// Preparing for email
$subject = "Happy Birthday from Hayward Baker";
$message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
$header = "From:" . "James.Lowley@hayward-bake
//echo $name;
mail ($email, $subject, $message, $header);
echo "Mail Sent.";
//echo $row['dob'];
}
echo "test6";
}
?>
Please ignore the tests
You need to pass an argument to the function like this:
function birthday($thing)
function birthday($thing)
http://www.laprbass.com/RAY_temp_booneball.php
Outputs:
39
Outputs:
39
<?php // RAY_temp_booneball.php
error_reporting(E_ALL);
// REQUIRED PHP 5.1+
date_default_timezone_set('America/Chicago');
// A FUNCTION TO RETURN THE AGE ON A GIVEN DATE
function your_age($birth_day, $test_date='Today')
{
// IF THE DATE STRINGS ARE NOT USABLE
if (!$b_timestamp = strtotime($birth_day)) return FALSE;
if (!$t_timestamp = strtotime($test_date)) return FALSE;
// GET THE YEARS AND SUBTRACT ONE
$years_elapsed = date('Y', $t_timestamp) - date('Y', $b_timestamp) - 1;
// IF THE BIRTHDAY IS BEFORE CUTOFF ADD ONE YEAR
if ( date('m-d', $b_timestamp) <= date('m-d', $t_timestamp) ) $years_elapsed++;
return $years_elapsed;
}
// SHOW THE FUNCTION IN ACTION
echo your_age('September 15, 1972');
Here is the script from a post a little above, showing some annotation and some references. You might want to consider taking a class in PHP or otherwise getting a foundation in the way the language works. It will make things go faster and easier if you understand the language basics.
line 5: You need to pass some kind of argument variable to the function.
http://us.php.net/manual/en/functions.user-defined.php
line 7: explode() requires some data to operate on (second argument).
http://us.php.net/manual/en/function.explode.php
line 11: The array $row is not defined in the variable scope of the function.
http://us.php.net/manual/en/language.variables.scope.php
line 21: The mysql_query() function returns a resource or FALSE into the variable $sql.
http://us.php.net/manual/en/function.mysql-query.php
line 22: The mysql_query() function expects a query string as input, but it gets whatever is in $sql and that cannot be a query string because of the preceding instruction. So the value in $result will be FALSE
http://us.php.net/manual/en/function.mysql-query.php
line 23: Before using $result in mysql_fetch_array() you want to test it to see if it is a results resource or FALSE. MySQL_Fetch_Array() expects a resource for its input argument.
http://us.php.net/manual/en/function.mysql-fetch-array.php
line 29: Oops! On line 23, we fetched the first row, so this while() loop will not see it. Thus we are missing a piece of the data, or are we? What results set do you really want to use? The variable names seem confused to me.
line 31-53: please read the article about Handling Date and Time in PHP and MySQL. Hint: It is easier than you think to get this stuff right!
line 67 and 69: Your code will be easier to read, understand, debug and modify if you line up the control structures and use a standard indentation for the conditional parts.
http://framework.zend.com/manual/en/coding-standard.html
line 5: You need to pass some kind of argument variable to the function.
http://us.php.net/manual/en/functions.user-defined.php
line 7: explode() requires some data to operate on (second argument).
http://us.php.net/manual/en/function.explode.php
line 11: The array $row is not defined in the variable scope of the function.
http://us.php.net/manual/en/language.variables.scope.php
line 21: The mysql_query() function returns a resource or FALSE into the variable $sql.
http://us.php.net/manual/en/function.mysql-query.php
line 22: The mysql_query() function expects a query string as input, but it gets whatever is in $sql and that cannot be a query string because of the preceding instruction. So the value in $result will be FALSE
http://us.php.net/manual/en/function.mysql-query.php
line 23: Before using $result in mysql_fetch_array() you want to test it to see if it is a results resource or FALSE. MySQL_Fetch_Array() expects a resource for its input argument.
http://us.php.net/manual/en/function.mysql-fetch-array.php
line 29: Oops! On line 23, we fetched the first row, so this while() loop will not see it. Thus we are missing a piece of the data, or are we? What results set do you really want to use? The variable names seem confused to me.
line 31-53: please read the article about Handling Date and Time in PHP and MySQL. Hint: It is easier than you think to get this stuff right!
line 67 and 69: Your code will be easier to read, understand, debug and modify if you line up the control structures and use a standard indentation for the conditional parts.
http://framework.zend.com/manual/en/coding-standard.html
<?PHP
include_once("../info_db/conn.php");
echo "test1";
function birthday ()
{
list($year,$month,$day) = explode("-");
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
$dob = date('Y-m-d',$row['dob']);
if ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
return $year_diff;
}
echo birthday($dob);
echo "test2";
//query for bdates in the database
$sql = mysql_query("SELECT u.dob, j.name, j.email FROM joomla.user_profile u, joomla.jos_users j WHERE j.id = u.id");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo "test3";
//Going through all the bdates
while($row = mysql_fetch_array($sql)){
// Run the function to grab the month and day only
$raw_bdates =($row['dob']);
$bdates_month = substr ($raw_bdates, 8, 2);
$bdates_year = substr ($raw_bdates, 2, 2);
$bdates = $bdates_year.$bdates_month;
$name=($row['name']);//your name field supposed as person_name
echo "test4";
// See what today is
$now = date("Ymd");
// Only grab the month and day from today
$now = substr($now, 4, 7);
//echo $bdates;
// Seeing if there is a match then email them a birthday message
//echo $now, $bdates."<br>";
if($now == $bdates) {
echo "Happy Birthday to $name!";
//echo $now;
$email = $row['email'] .","; // to be checked
//echo $email;
echo "test5";
// Preparing for email
$subject = "Happy Birthday from Hayward Baker";
$message = "HAVE A GREAT BIRTHDAY".$name; // to be changed
$header = "From:" . "James.Lowley@hayward-baker.com";
//echo $name;
mail ($email, $subject, $message, $header);
echo "Mail Sent.";
//echo $row['dob'];
}
echo "test6";
}
?>
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks Again Ray!
remove the last } behind your code.
tip: when testing code, enable php error reporting: error_reporting(E_ALL);
Hope this helps !