Adam Walker
Adam Walker

Reputation: 41

What causes "undefined function" date_diff?

i have this script which i'm using in my php to convert a users date of birth to their age. It works fine on my local host but when i upload it to the server the page comes up with this error:

Fatal error: Call to undefined function date_diff() in /home/content/31/9118831/html/projects/mark.ptb/PTB1/includes/mod_home/mod_platinum.php on line 19

Here's my code, i'm guessing i've not defined the function although i can't understand why it would work fine on my localhost therefore?

<?php
 $dob = $platinum['dob'];

function age_from_dob($dob) {


       $age = date_diff(date_create($dob), date_create('now'))->y;  
       return $age;
}

?>

<?

        $platinum_set = get_platinum_users();
        while ($platinum = mysql_fetch_array($platinum_set)) {
        $age = age_from_dob($platinum['dob']);
             echo "
            <div class=\"platinumcase\">
            <a href=\"profile.php?id={$platinum['id']}\"><img width=80px height= 80px src=\"data/photos/{$platinum['id']}/_default.jpg\" class=\"boxgrid\"/></a><h58> {$platinum['first_name']} {$platinum['last_name']}</h58><br/><br/><h52> ".$age." Years Old<br/><br/>From {$platinum['location']}</h52>

            </div>";
        }
    ?>

Upvotes: 1

Views: 5021

Answers (4)

Codemaker2015
Codemaker2015

Reputation: 1

Please use date object instead of date_diff() function. you can use $date->diff() function to calculate date difference For example:

$fromdate = new DateTime($todate); $diff = $fromdate->diff('now')->y;

Upvotes: 0

rid
rid

Reputation: 63442

The date_diff() function is part of PHP >= 5.3. If it doesn't exist on the server but it does on your local machine, then the server is using an older version of PHP while you're using PHP >= 5.3.

It's a good idea to always develop using the same version of PHP as the deployment environment. To find out what PHP version you're using, you can echo the PHP_VERSION constant.

Upvotes: 1

zackg
zackg

Reputation: 329

According to the php manual, date_diff is only available in PHP 5.3+

Is the version of PHP on the server sufficient? You can use phpinfo() command to determine what version of PHP is being used on the server.

Upvotes: 1

maček
maček

Reputation: 77778

You're doing it just a bit wrong :)

$date = new DateTime($dob);
$diff = $date->diff('now')->y;

Make sure to catch errors as the user-specified $dob string may not always result in a valid date.

Upvotes: 0

Related Questions