3

I want to covert time() which PHP has created and stored in our database to minutes ago when it is captured by our JavaScript once requested by JSON.

http://media.queerdio.com/mobileDevice/?uri=nowplaying/1

as you can see we store time like

"airtime":1382526339

what we want to turn that to is

3 minutes ago

This is what we know.

We first would need to run something like this

function convert_time(ts) {
   return new Date(ts * 1000) 
}

Taking the airtime and running it through that function this makes it JavaScript compliant from what I read (I could be wrong) Unix time javascript

Bt then I am not sure how to get it from JavaScript date time to minutes ago. We currently don't use jQuery and would be great if we could continue down that road, as this is the last issue I am having before I have finished coding.

halfer
  • 19,824
  • 17
  • 99
  • 186
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

5 Answers5

12

No need for any dependency, just plain old javascript is sufficient. To convert a unix time stamp to a "X time ago"-like label, you simply use something like this:

function time2TimeAgo(ts) {
    // This function computes the delta between the
    // provided timestamp and the current time, then test
    // the delta for predefined ranges.

    var d=new Date();  // Gets the current time
    var nowTs = Math.floor(d.getTime()/1000); // getTime() returns milliseconds, and we need seconds, hence the Math.floor and division by 1000
    var seconds = nowTs-ts;

    // more that two days
    if (seconds > 2*24*3600) {
       return "a few days ago";
    }
    // a day
    if (seconds > 24*3600) {
       return "yesterday";
    }

    if (seconds > 3600) {
       return "a few hours ago";
    }
    if (seconds > 1800) {
       return "Half an hour ago";
    }
    if (seconds > 60) {
       return Math.floor(seconds/60) + " minutes ago";
    }
}

Of course you can change the texts/ranges as you see fit.

Hope this will help, my point is that you don't need to use any library to achieve this kind of things :)

sitifensys
  • 2,024
  • 16
  • 29
  • 1
    perfect, Thank you so much simple and did not add 8.8kb to my script. – RussellHarrower Oct 23 '13 at 11:45
  • you're welcome, I will never understand people that love to add bloated libraries just because they're too lazy to think about a simple solution. – sitifensys Oct 23 '13 at 12:08
  • 1
    @sitifensys That's quite an interesting POV. My POV is that some people are just too lazy to look for a library that serves their needs and instead prefer to reinvent the wheel. I think that's also called the [NIH](http://en.wikipedia.org/wiki/Not_invented_here) phenomenon. The real costs of your approach in any aspect of the matter (QA, testing, extensibility, x-browser-compat, ...) will _never_ be covered by those 8.8k saved in terms of transfered data. – mwhs Oct 23 '13 at 12:34
  • @mwhs Our two POVs are not incompatible. Mine is not a "systematically reinvent the wheel" but rather a "you should always keep it as simple as possible". While I do understand a developer going for a well tested and established framework to have a solid application foundation, I don't understand a guy adding *JQuery*+*JQueryUI*+*JQueryWhateverSuperCoolCandendarWidget* just to add a simple drop down calendar to a inputbox :). – sitifensys Oct 23 '13 at 12:50
  • @sitifensys I see you totally got my point. That is quite consistent with the motto "do less, write more" :-) – mwhs Oct 23 '13 at 19:55
  • How to show hours ago if i pass this function unix timestamp? – user1788736 Jul 02 '20 at 07:52
5

I would suggest you Moment.js a small (8.8 kb minified) javascript library for formatting date objects. It works really well and has no further dependencies.

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
2

You can use something like this if you need more detail:

function timeAgo(someDateInThePast) {
    var result = '';
    var difference = Date.now() - someDateInThePast;

    if (difference < 5 * 1000) {
        return 'just now';
    } else if (difference < 90 * 1000) {
        return 'moments ago';
    }

    //it has minutes
    if ((difference % 1000 * 3600) > 0) {
        if (Math.floor(difference / 1000 / 60 % 60) > 0) {
            let s = Math.floor(difference / 1000 / 60 % 60) == 1 ? '' : 's';
            result = `${Math.floor(difference / 1000 / 60 % 60)} minute${s} `;
        }
    }

    //it has hours
    if ((difference % 1000 * 3600 * 60) > 0) {
        if (Math.floor(difference / 1000 / 60 / 60 % 24) > 0) {
            let s = Math.floor(difference / 1000 / 60 / 60 % 24) == 1 ? '' : 's';
            result = `${Math.floor(difference / 1000 / 60 / 60 % 24)} hour${s}${result == '' ? '' : ','} ` + result;
        }
    }

    //it has days
    if ((difference % 1000 * 3600 * 60 * 24) > 0) {
        if (Math.floor(difference / 1000 / 60 / 60 / 24) > 0) {
            let s = Math.floor(difference / 1000 / 60 / 60 / 24) == 1 ? '' : 's';
            result = `${Math.floor(difference / 1000 / 60 / 60 / 24)} day${s}${result == '' ? '' : ','} ` + result;
        }

    }

    return result + ' ago';
}

document.write(timeAgo(Date.parse('2019-10-10 13:10')));

https://github.com/carmatas/timeago

0

I would recommend to use Datejs. It extends the browser its native Date object.

hvdd
  • 514
  • 4
  • 7
0

In response to the last comment of Russell:

If you use Moment.js with a unix timestamp (epoch in seconds) then say

moment.unix(unixtime).startOf('hour').fromNow()

otherwise if you use a milliseconds timestamp (java System.currentTimeMillis()) then say

moment(millis).startOf('hour').fromNow()

See the documentation here: http://momentjs.com/docs/#/parsing/unix-offset/

mwhs
  • 5,878
  • 2
  • 28
  • 34