Question

I pulling date from Database. Time Ago is working fine but its not update time

<abbr class="timer timeago modified"><?php echo date('Y-m-d H:i:s', strtotime($row['project_create_date'])); ?></abbr>

JQuery

$(function() {    
    /*Time Ago*/

    prepareDynamicDates();
    var time = '';
    $('.timer').each(function(){
        var time = $(this).text();
        $(this).text(jQuery.timeago(time));
   });

});
Was it helpful?

Solution

First of all you need to use a data parameter to store the reference date. Using text will get overwritten on the first instantiation.

<abbr class="timer timeago modified" data-create-date="2012-11-05 14:50:11"></abbr>

Then you can use setInterval to update the text as needed:

var time = '';
$('.timer').each(function() {
    var $el = $(this);
    setInterval(function() {
        var time = $el.data("create-date");
        $el.text(jQuery.timeago(time));
    }, 1000); // 1000ms = 1 second
});

Example fiddle

Note this updates every second which may cause poor performance in some browsers. I'd have thought refreshing every 30 seconds to 1 minute should be sufficient for what you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top