当我在页面上发表评论时,我需要获得两个时间戳之间的差异


<?php
session_start();
function time_stamp($session_time) 
{ 
$session_time=new DateTime();
$post_time=new DateTime();
$time_difference = date_diff($post_time,$session_time); 
$time_difference->format('y-m-d h-m-s');
$seconds = $time_difference ; 
$minutes = round($time_difference / 60 ); //line 11
$hours = round($time_difference / 3600 ); //line 12
$days = round($time_difference / 86400 ); //line 13
$weeks = round($time_difference / 604800 ); //line 14
$months = round($time_difference / 2419200 ); //line 15
$years = round($time_difference / 29030400 ); //line 16
if($seconds <= 60)
{
echo"$seconds seconds ago";  //line 18
}
else if($minutes <=60)
{
if($minutes==1)
{
echo"one minute ago"; 
}
else
{
echo"$minutes minutes ago"; 
}
?>

我试图获得两个时间戳之间的差异,即当帖子发布和当前时间。当我提交我的帖子时,它应该显示发布的x秒/分钟/周前

错误消息

注意:在C:xampphtdocssocialhome.php的第11行中,DateInterval类的对象不能转换为int注意:DateInterval类的对象不能转换为intC:xampphtdocssocialhome.php第12行注意:在C:xampphtdocssocialhome.php第13行中,无法将类DateInterval的对象转换为int注意:在C:xampphtdocssocialhome.php的第14行中,DateInterval类的对象无法转换为int注意:在C:xampphtdocssocialhome.php第15行中,DateInterval类的对象无法转换为int注意:在C:xampphtdocssocialhome.php第16行中,DateInterval类的对象无法转换为int注意:在C:xampphtdocssocialhome.php的第18行中,DateInterval类的对象无法转换为int可恢复的致命错误:在C:xampphtdocssocialhome.php的第20行中,DateInterval类的对象无法转换为字符串

public DateInterval::format ( string $format ) : string

https://www.php.net/manual/en/dateinterval.format.php

格式函数(第10行),将返回一个字符串,所以我不确定为什么你使用它来获得$seconds

相反,date_diff返回一个DateInterval对象,因此您应该查看php文档中的DateIntervalhttps://www.php.net/manual/en/class.dateinterval.php

你可能会注意到它有一些性质,如:$y,$m等。所以你应该做的实际上是$time_difference->s

最新更新