Hi i am getting the following error in a php program i am using

PHP Warning: A non-numeric value encountered in xxxxxxx db_connect.php on line 60

// find age
if(! function_exists('Age') ) 
{
    function Age($birthdate)
    { 
            return date("Y") - substr(trim($birthdate),-4,4);               //// THIS IS THE LINE FOR THE SAID ERROR
    }
}

if anyone could give advice or solution on this many thanks x

As rproffitt points out, it's possible that $birthdate is in an incorrect format and you think you're retrieving a year, but you're really not. For example, if my $birthdate was set to 11-11-82 then it will retrieve 11-82 instead of 1982.

You should make sure that $birthdate is in an appropriate format before calculating age.

What I would suggest doing is changing your code to something like this:

// find age
if(! function_exists('Age') ) 
{
    function Age($birthdate)
    { 

           // Break the date in the format YYYY-MM-DD apart into a year, month, and day
            list($year , $month, $day) = explode('-', $birthdate);

            if (checkdate($month, $day, $year))
            {
                       // The birthdate is a valid date

                        // Create a PHP Date object for the birthday
                        $date_obj = new DateTime("$year-$month-$day");

                        // Create a PHP date object for the current date
                        $now_obj = new DateTime();

                        // Check to make sure that the birthday is a date older than today
                        if ($date_obj > $now_obj)
                        {
                            return date("Y") - substr(trim($birthdate),-4,4);               //// THIS IS THE LINE FOR THE SAID ERROR
                        }
                        else
                        {
                            // Birthdate is in the past
                            return 0;
                        }
            }
            else
            {
                // Invalid birthdate specified
                return 0;
            }
    }
}

Doing something like that not only ensures that the $birthdate variable is in the correct format, but it also ensures that a user doesn't specify an invalid date for their birthday, which means either a future date, or a date that never existed such as February 29th in a non-leap year or September 31st (September has 30 days).

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.