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

Susan_WM
 <?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:\xampp\htdocs\social\home.php on line 11

Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\social\home.php on line 12

Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\social\home.php on line 13

Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\social\home.php on line 14

Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\social\home.php on line 15

Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\social\home.php on line 16

Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\social\home.php on line 18

Recoverable fatal error: Object of class DateInterval could not be converted to string in C:\xampp\htdocs\social\home.php on line 20 **

RonaldoC
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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Number of days between two time stamps

分類Dev

When should I use GET or POST method? What's the difference between them?

分類Dev

When should I use GET or POST method? What's the difference between them?

分類Dev

When should I use GET or POST method? What's the difference between them?

分類Dev

Calculate the difference between two stamps based on another column

分類Dev

To get logs in between particular time-stamps using shell script

分類Dev

Comparing Two Time stamps in PHP

分類Dev

How can I avoid the difference of milliseconds between two Chronometers start time?

分類Dev

Pandas: Calculate running difference between prior record between date-time stamps

分類Dev

time difference between date[i + 1] and date[i] by group

分類Dev

How do I get the difference in minutes between two dates in SSIS using Derived Column transform?

分類Dev

Do I need to use the same VM in the POST, as the GET, to reload index?

分類Dev

Why do I get an undefined variable error when trying to use PHP in two sections of a page?

分類Dev

How can I get absolute difference of two numbers?

分類Dev

How do I get a set difference between 2 observables?

分類Dev

Can I get gradle build to fail when there is version conflict between two dependencies when one of them doesn't have group specified

分類Dev

Get real time updates from a Facebook page I do not own

分類Dev

Go: error getting difference between two time

分類Dev

Fill panel when there are two i identifiers and one t time

分類Dev

Find closest timestamps between two dataframes and merge different columns when time difference is < 60s

分類Dev

How to get the difference between two date and times and show the time running down

分類Dev

When I publish a new post in Wordpress, I cannot get the tags of the post

分類Dev

Why is WebAPI action found as GET but 404 when I use POST?

分類Dev

How to subtract two time stamps along with their dates in bash?

分類Dev

Using time stamps in one table as an interval for a select between in another

分類Dev

If I can easily fetch records by join of two tables. Then why we need relationship between tables

分類Dev

Get the difference between two arrays in Laravel

分類Dev

Get difference between two days (minus method)

分類Dev

How do I remove devEdpress notification appears every time when I reload the page

Related 関連記事

  1. 1

    Number of days between two time stamps

  2. 2

    When should I use GET or POST method? What's the difference between them?

  3. 3

    When should I use GET or POST method? What's the difference between them?

  4. 4

    When should I use GET or POST method? What's the difference between them?

  5. 5

    Calculate the difference between two stamps based on another column

  6. 6

    To get logs in between particular time-stamps using shell script

  7. 7

    Comparing Two Time stamps in PHP

  8. 8

    How can I avoid the difference of milliseconds between two Chronometers start time?

  9. 9

    Pandas: Calculate running difference between prior record between date-time stamps

  10. 10

    time difference between date[i + 1] and date[i] by group

  11. 11

    How do I get the difference in minutes between two dates in SSIS using Derived Column transform?

  12. 12

    Do I need to use the same VM in the POST, as the GET, to reload index?

  13. 13

    Why do I get an undefined variable error when trying to use PHP in two sections of a page?

  14. 14

    How can I get absolute difference of two numbers?

  15. 15

    How do I get a set difference between 2 observables?

  16. 16

    Can I get gradle build to fail when there is version conflict between two dependencies when one of them doesn't have group specified

  17. 17

    Get real time updates from a Facebook page I do not own

  18. 18

    Go: error getting difference between two time

  19. 19

    Fill panel when there are two i identifiers and one t time

  20. 20

    Find closest timestamps between two dataframes and merge different columns when time difference is < 60s

  21. 21

    How to get the difference between two date and times and show the time running down

  22. 22

    When I publish a new post in Wordpress, I cannot get the tags of the post

  23. 23

    Why is WebAPI action found as GET but 404 when I use POST?

  24. 24

    How to subtract two time stamps along with their dates in bash?

  25. 25

    Using time stamps in one table as an interval for a select between in another

  26. 26

    If I can easily fetch records by join of two tables. Then why we need relationship between tables

  27. 27

    Get the difference between two arrays in Laravel

  28. 28

    Get difference between two days (minus method)

  29. 29

    How do I remove devEdpress notification appears every time when I reload the page

ホットタグ

アーカイブ