-2

I need help for a web application which consists of JavaScript or PHP which can output "2 seconds ago, 1min ago" likewise using a time counter, after a certain post. Please help me with this.

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
  • You probably have to use PHP to keep track of the time outside the browser of each user. Good luck. – adeneo Dec 12 '15 at 13:31
  • http://momentjs.com/ – Tomasz Kowalczyk Dec 12 '15 at 13:32
  • 1
    Welcome to Stack Overflow. We’d love to help you. To improve your chances of getting an answer, here are some tips: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Paul Roub Dec 12 '15 at 13:40
  • there is a simple function that converts a date into a prettydiff or human-diff like 'just now', one 'minute ago' etc.. it is very simple (e.g https://github.com/phstc/jquery-dateFormat) Then you will have to output the post date with PHP and let JS take that date, parse it and convert it into a humane-diff format, (or let PHP do that from the start, just implement the function in PHP, http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – Nikos M. Dec 12 '15 at 13:44

1 Answers1

2

You can try this with this PHP function and for the javascript version of Time Ago implementation http://momentjs.com/ is fantastic.

function get_timeago( $ptime )
{
    $estimate_time = time() - $ptime;

    if( $estimate_time < 1 )
    {
        return 'less than 1 second ago';
    }

    $condition = array( 
                12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $estimate_time / $secs;

        if( $d >= 1 )
        {
            $r = round( $d );
            return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
        }
    }
}

//You can use the normal date function to pass it to
//I have tried with e.g 2015-12-12 08:57:00

$timeago=get_timeago(strtotime('2015-12-12 08:57:00'));
echo $timeago;
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103