日期时间差异在 Wordpress 页面中无法正常工作
全部, 我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我记得不久前这个问题:如何计算使用 PHP 的两个日期之间的差异?
通过一些小的调整,它看起来像这样:
输出:
从此站点修改的数字到字母函数: 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:
Output:
Number to letters function modified from this site: http://www.phpro.org/examples/Convert-Numbers-to-Words.html
正如手册中所述,
DateTime::Diff
(以及所有DateInterval
函数)仅在 PHP >= 5.3.0 中可用。您需要安装该版本,或者找到使用另一组功能的解决方法。 这里是如何使用旧的基于时间戳的日期函数执行此操作的示例。
As said in the manual,
DateTime::Diff
(and all theDateInterval
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.