php$diff->;的问题;总体安排


issue with php $diff->format

我得到一个"Fatal error: Call to a member function format() on a non-object in /home/mrbits/public_html/tickets/index2.php on line 407",代码如下:

Xxx
Xxxx
xxxx
..
$t_enter=$row['fecha_ing'];   <- fields is type DATE
$yesterday=strtotime($t_enter);
$today_is = date("Y/m/d");
$diff=date_diff($yesterday,$today_is);
$t_elapsed= $diff->format("%y A %m m %d d");     <--- HERE's the ERROR
?>
<td><?php echo $t_idticket; ?></td>
<td><?php echo $t_elapsed; ?></td>

可能出了什么问题?表日期中变量的类型?

您的类型不匹配。$yesterday是一个整数,而$today_is是一个字符串。您必须执行一个strtotime($today_is);,因为$date_diff();需要2个DateTime对象,所以您需要额外的转换。

使用日期创建:

$t_enter=$row['fecha_ing'];  
$yesterday=date_create($t_enter);
$today_is = date_create(date("Y/m/d"));
$diff=date_diff($yesterday,$today_is);
$t_elapsed= $diff->format("%y A %m m %d d");