1

Why the following code:

function dDiff($start, $end = false)
{
    if(!$end)
    {
        $end = time();
    }

    if(!is_numeric($start) || !is_numeric($end))
    {
        return false;
    }

    $start  = date('Y-m-d H:i:s',$start); 
    $end    = date('Y-m-d H:i:s',$end); 
    $d_start    = new DateTime($start);
    $d_end      = new DateTime($end);
    $diff = $d_start->diff($d_end);

    return array(
        'year' => $diff->format('%y'),
        'month' => $diff->format('%m'),
        'day' => $diff->format('%d'),
        'hour' => $diff->format('%h'),
        'min' => $diff->format('%i'),
        'sec' => $diff->format('%s')
    );
}

produce that error :

Fatal error: Call to undefined method DateTime::diff()
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
  • 4
    Are you sure you run PHP version 5.3? DateTime class was added in 5.2 but the diff method is available only since version 5.3. See http://www.php.net/manual/en/datetime.construct.php http://www.php.net/manual/en/datetime.diff.php – poisson Oct 29 '11 at 11:28
  • I have create a WordPress plugin, and the users of the plugin they reporting that error. In case that I like to find the diference of two dates what else method can I use ? – KodeFor.Me Oct 29 '11 at 11:31
  • @merianos-nikos You can either put a constraint on that plugin that it requires PHP version 5.3. That would make sense if you also use another features from 5.3 or you can rewrite your dDiff function so it doesn't use DateTime:diff(). It won't be that difficult. You already have start and end in "unix seconds" so if you simply subtract them you get the difference in seconds. From that you get mins,hrs and days easily.Months and years will be a bit trickier but you might getaway with counting month as 30 days and year 12months or so. – poisson Oct 29 '11 at 11:41
  • 1
    Btw what you really need is StackOverflow and searching: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php :) – poisson Oct 29 '11 at 11:43
  • 1
    @MerianosNikos: First of all tell your plugin users, that they need PHP 5.3 or higher for your plugin to run. That's important so you don't create any misunderstandings. Then insert a routine that checks the PHP version and display them a notice that it does not work if the PHP version is too small. *Then* think about if you want to re-code PHP functionality and if you want to, do it and paste your code here. See as well related questions, I'm pretty sure you're not the first one asking for how to do this. – hakre Oct 29 '11 at 11:43

1 Answers1

1

You get the error because the diff() function does not exists on the DateTime object. That's the case with PHP versions below 5.3 In the manual that's written as

(PHP 5 >= 5.3.0)

on top of each function page.

You either need to make PHP 5.3 a requirement for your plugin and/or switch to an alternative routine for that part. See How to calculate the difference between two dates using PHP?.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836