【问题标题】:DateTime Catchable fatal error [duplicate]DateTime 可捕获的致命错误 [重复]
【发布时间】:2016-06-08 11:27:06
【问题描述】:

我有问题,我是初学者 我有一个生日日期(格式为 Y-m-d) 我有实际的日期。

$date = $_POST["DatumJJJJ"]."-".$_POST["DatumMM"]."-".$_POST["DatumTT"];
$birthday = new DateTime($date);
$now = new DateTime(date("Y-m-d"));
$difference = $birthday->diff($now);
echo $difference;

现在,最后一行出现错误:

Catchable fatal error: Object of class DateInterval could not be converted to string

我该怎么办?我看到了其他类似的问题,但它们对我没有帮助!

【问题讨论】:

标签: php


【解决方案1】:

这是因为$difference 不是字符串。您不能像打印字符串一样直接打印它。如果要查看$difference 包含的数据。使用print_r()。像这样,

print_r($difference);

上述函数的输出是这样的,

DateInterval Object
(
    [y] => 0
    [m] => 7
    [d] => 29
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 242
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

有关对象内每个键的详细信息,请参阅http://php.net/manual/en/class.dateinterval.php

【讨论】:

  • 谢谢,但现在我还有一个问题
  • 我试过$difference->format(Y);$difference->format(Y-M-D); 但它不起作用。我只想要年月日
  • 你可以这样得到它:$difference->y, $difference->m and $difference->d
猜你喜欢
  • 1970-01-01
  • 2021-07-08
  • 2023-01-19
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多