0

I have a date field in mysql that has datetime in the below period :

2014-05-31 15:00:55

I want to convert this value to "passed time" till current time.

for example if two hours are passed till the saved time,then I want something like this :

two hours ago

if three days are passed till the saved time,then it should be:

three days ago

I am trying to display the output with php as following :

<?php
echo '<p align="right">';
echo $row2['date'] ;
echo '</p>';
?>

I am new in PHP,Can anyone help me out.

user3318980
  • 39
  • 2
  • 7
  • Possible duplicate: http://stackoverflow.com/questions/6108819/javascript-timestamp-to-relative-time-eg-2-seconds-ago-one-week-ago-etc-best ; http://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time – hindmost Jun 01 '14 at 07:03
  • Try this URLS this might be helpful to you, [http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time][1] [http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site][1] – akhilesh0707 Jun 01 '14 at 07:07

1 Answers1

0

You could try something like this:

function timeDiff(current, previous) {

    var msPerMin = 60 * 1000;
    var msPerHr = msPerMin * 60;
    var msPerDay = msPerHr * 24;
    var msPerMonth = msPerDay * 30;
    var msPerYear = msPerDay * 365;

    var elapsed = current - previous;

    if (elapsed < msPerMin) {
         return Math.round(elapsed / 1000) + ' seconds ago';   
    }

    else if (elapsed < msPerHr) {
         return Math.round(elapsed / msPerMin) + ' minutes ago';   
    }

    else if (elapsed < msPerDay ) {
         return Math.round(elapsed / msPerHr ) + ' hours ago';   
    }

    else if (elapsed < msPerMonth) {
        return 'approximately ' + Math.round(elapsed / msPerDay) + ' days ago';   
    }

    else if (elapsed < msPerYear) {
        return 'approximately ' + Math.round(elapsed / msPerMonth) + ' months ago';   
    }

    else {
        return 'approximately ' + Math.round(elapsed / msPerYear ) + ' years ago';   
    }
}

Source: Javascript timestamp to relative time (eg 2 seconds ago, one week ago etc), best methods?


This is how you implement it:

var now = new Date();
var someDate = new Date(2011, 04, 24, 12, 30, 00, 00);
alert(timeDiff(now, someDate));

Fiddle.

Community
  • 1
  • 1
chris97ong
  • 6,870
  • 7
  • 32
  • 52