【问题标题】:I need to get the difference between two time stamps when I post comment on the page当我在页面上发表评论时,我需要获取两个时间戳之间的差异
【发布时间】: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 中转换为字符串

【问题讨论】:

标签: php post timestamp


【解决方案1】:
public DateInterval::format ( string $format ) : string

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

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

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

您可能会注意到它具有一些属性,例如:$y$m 等。 所以你应该做的实际上是$time_difference-&gt;s

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2020-05-13
    相关资源
    最近更新 更多