日期时间差异在 Wordpress 页面中无法正常工作

发布于 2024-12-25 04:20:26 字数 501 浏览 0 评论 0原文

全部, 我有以下 PHP 代码来确定我的 Wordpress 页面上两个日期相隔多少年:

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

我为 Wordpress 安装了 Exec-PHP 插件以正确显示它。但是,当我尝试显示我的页面时,出现以下错误:

致命错误:调用未定义的方法 DateTime::diff() /home/person/test.website.com/wp-content/plugins/exec-php/includes/runtime.php(42) :第 7 行 eval() 代码

如何才能使其正常工作?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2025-01-01 04:20:26

我记得不久前这个问题:如何计算使用 PHP 的两个日期之间的差异?

通过一些小的调整,它看起来像这样:

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");

输出:

4
2

从此站点修改的数字到字母函数: http://www.phpro.org/examples/Convert-Numbers-to-Words.html

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

这样的小城市 2025-01-01 04:20:26

正如手册中所述,DateTime::Diff (以及所有 DateInterval 函数)仅在 PHP >= 5.3.0 中可用。

您需要安装该版本,或者找到使用另一组功能的解决方法。 这里是如何使用旧的基于时间戳的日期函数执行此操作的示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文