Searching the web, I found an article, where it shows an example of how to show the time of a post, similar to facebook.
The code works perfectly, when executed.
<?php
date_default_timezone_set('America/New_York');
echo facebook_time_ago('2016-03-11 04:58:00');
function facebook_time_ago($timestamp)
{
$time_ago = strtotime($timestamp);
$current_time = time();
$time_difference = $current_time - $time_ago;
$seconds = $time_difference;
$minutes = round($seconds / 60 ); // value 60 is seconds
$hours = round($seconds / 3600); //value 3600 is 60 minutes * 60 sec
$days = round($seconds / 86400); //86400 = 24 * 60 * 60;
$weeks = round($seconds / 604800); // 7*24*60*60;
$months = round($seconds / 2629440); //((365+365+365+365+366)/5/12)*24*60*60
$years = round($seconds / 31553280); //(365+365+365+365+366)/5 * 24 * 60 * 60
if($seconds <= 60)
{
return "Just Now";
}
else if($minutes <=60)
{
if($minutes==1)
{
return "one minute ago";
}
else
{
return "$minutes minutes ago";
}
}
else if($hours <=24)
{
if($hours==1)
{
return "an hour ago";
}
else
{
return "$hours hrs ago";
}
}
else if($days <= 7)
{
if($days==1)
{
return "yesterday";
}
else
{
return "$days days ago";
}
}
else if($weeks <= 4.3) //4.3 == 52/12
{
if($weeks==1)
{
return "a week ago";
}
else
{
return "$weeks weeks ago";
}
}
else if($months <=12)
{
if($months==1)
{
return "a month ago";
}
else
{
return "$months months ago";
}
}
else
{
if($years==1)
{
return "one year ago";
}
else
{
return "$years years ago";
}
}
}
?>
Time is displayed correctly
<li><a href="#"><i class="fa fa-clock-o"></i>'.facebook_time_ago($data_time).'</a></li>
The problem is that I have to refresh the page to see the elapsed time of the post.
How can I show the real time of it without having to refresh the page, just as it is used by stackoveflow or facebook?
Since the code is in PHP, the simplest would be to use Ajax .
In other words, at a certain time interval you make a request to the server to return the times, and when they arrive, you display it.
For intervals you can use setInterval , and define how often a request should be made to the server to update the times.
The Ajax call (asynchronous Javascript) will make a GET or POST request to the PHP script that you indicate. Let's say your script is called
tiempo.php
, so you make an asynchronous GET request to it that will return the time in plain text, for example.The response
echo
from the server will be the one you can display in the HTML listing object.As I put in a comment, don't use PHP for that, use JavaScript to be able to update the values from the client side without having to reload the page or make calls to the server with AJAX.
The idea would be to put the date of the post in a data-attribute (with an easy-to-read format with Javascript such as YYYY-MM-DD HH:mm:SS" and update the value every so often. This is how I did it before Facebook (and still seems to be) and Stack Overflow also seems to use a similar system.
You could adapt the function you already have in PHP and transform it to JavaScript, then call it every 30 seconds (for example) so that the time value is updated. Something like this: