简体   繁体   中英

DateTime diff not working correctly in Wordpress page

All, I have the following PHP code to determine how many years apart two dates are on my Wordpress page:

<?php
$date1 = new DateTime("2003-03-24");
$current_date = new DateTime(date("Y-m-d"));
$interval = $date1->diff($current_date);
echo $interval->y;
?>

I installed the Exec-PHP plugin for Wordpress to display this properly. However when I try and display my page I get the following error:

Fatal error: Call to undefined method DateTime::diff() in /home/person/test.website.com/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 7

How can I get this to work properly? Thanks!

As said in the manual , DateTime::Diff (and all the DateInterval functions) is available in PHP >= 5.3.0 only.

You would need to install that version, or find a workaround that uses another set of functions. Here is an example for how to do this using the old timestamp-based date functions.

I remember this question a while back: How to calculate the difference between two dates using PHP?

With some small adjustments it looks like this:

function convert_number($number) { 
    $Gn = floor($number / 1000000);  /* Millions (giga) */ 
    $number -= $Gn * 1000000; 
    $kn = floor($number / 1000);     /* Thousands (kilo) */ 
    $number -= $kn * 1000; 
    $Hn = floor($number / 100);      /* Hundreds (hecto) */ 
    $number -= $Hn * 100; 
    $Dn = floor($number / 10);       /* Tens (deca) */ 
    $n = $number % 10;               /* Ones */ 

    $res = ""; 

    if ($Gn)
        $res .= convert_number($Gn) . " Million"; 
    if ($kn)    { 
        $res .= (empty($res) ? "" : " ") . 
        convert_number($kn) . " Thousand"; 
    } 

    if ($Hn) { 
        $res .= (empty($res) ? "" : " ") . 
        convert_number($Hn) . " Hundred"; 
    } 

    $ones = array("", "One", "Two", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen","Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen","Nineteen"); 
    $tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty","Seventy", "Eigthy", "Ninety"); 

    if ($Dn || $n)  { 
        if (!empty($res))
            $res .= " and "; 

        if ($Dn < 2) 
        $res .= $ones[$Dn * 10 + $n]; 
        else { 
            $res .= $tens[$Dn]; 

            if ($n) 

            $res .= "-" . $ones[$n]; 
        } 
    } 

    if (empty($res)) 
        $res = "zero"; 

    return $res; 
} 

function yearsFromNow ($date) {
    return convert_number(floor(abs(strtotime($date) - strtotime(date("Y-m-d"))) / (365*60*60*24)));
}

echo yearsFromNow("2007-03-24");
echo yearsFromNow("2009-06-26");

Output:

4
2

Number to letters function modified from this site: http://www.phpro.org/examples/Convert-Numbers-to-Words.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM