0

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!

Manse
  • 37,765
  • 10
  • 83
  • 108
user1048676
  • 9,756
  • 26
  • 83
  • 120

2 Answers2

1

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.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

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

Community
  • 1
  • 1
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • thank you very much that works good. The answer I got was 8. Is there anyway to make this read eight or four etc depending on what is returned? – user1048676 Jan 06 '12 at 15:08
  • What do you mean by "read eight or four"? With text instead of numbers? – OptimusCrime Jan 06 '12 at 15:11
  • I'm not really sure I see the point of this code - what's its advantage over something like `ceil ((time() - strtotime("2007-03-24")) / (60* 60 * 24 *365))`? – Pekka Jan 06 '12 at 16:25