Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

php - I need to get the difference between two time stamps when I post comment on the page

 <?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"; 
   }
     ?>

** When I submit my post it should show x seconds/minutes etc ago

ERROR MESSAGE Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 11

Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 12

Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 13

Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 14

Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 15

Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 16

Notice: Object of class DateInterval could not be converted to int in C:xampphtdocssocialhome.php on line 18

Recoverable fatal error: Object of class DateInterval could not be converted to string in C:xampphtdocssocialhome.php on line 20 **

question from:https://stackoverflow.com/questions/65941760/i-need-to-get-the-difference-between-two-time-stamps-when-i-post-comment-on-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public?DateInterval::format?(?string?$format?) :?string

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

The format function (line 10), will return a string , so I'm not sure why you're using that to get the $seconds

Instead, date_diff returns a DateInterval object, so you should look at the php docs for DateInterval https://www.php.net/manual/en/class.dateinterval.php

And you may notice that it has some properties such as: $y, $m, etc. So what you should do is actually something like $time_difference->s.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...