【发布时间】:2021-01-28 16:59:04
【问题描述】:
<?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 秒/分钟/周前发布的内容
错误信息
注意:DateInterval 类的对象无法在第 11 行的 C:\xampp\htdocs\social\home.php 中转换为 int 注意:类 DateInterval 的对象无法转换为 int in C:\xampp\htdocs\social\home.php 第 12 行 注意:DateInterval 类的对象无法在第 13 行的 C:\xampp\htdocs\social\home.php 中转换为 int 注意:第 14 行的 C:\xampp\htdocs\social\home.php 中的类 DateInterval 的对象无法转换为 int 注意:DateInterval 类的对象在第 15 行的 C:\xampp\htdocs\social\home.php 中无法转换为 int 注意:DateInterval 类的对象无法在第 16 行的 C:\xampp\htdocs\social\home.php 中转换为 int 注意:DateInterval 类的对象无法在第 18 行的 C:\xampp\htdocs\social\home.php 中转换为 int 可恢复的致命错误:DateInterval 类的对象无法在第 20 行的 C:\xampp\htdocs\social\home.php 中转换为字符串
【问题讨论】:
-
您似乎从根本上误解了
date_diff返回的内容。正如错误本身明确指出的那样,它是一个DateIntervalobject,您将其视为一个简单的数字。 -
$time_difference不是整数。 php.net/manual/en/datetime.diff.php#97880可能是你想要的