Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a unix time stamp to twitter/facebook style

I'm trying to convert a unix time stamp to display like facebook and twitter. For example, when you see tweets or comments placed on twitter/facebook you see the date/time displayed like so:

'2 mins ago' or '2 days ago' or '2 weeks ago'

Does anyone one know of any function to get it working like this. I'm guessing it will be a custom one.

Any help is much appreciated

like image 200
HomeBrew Avatar asked Jul 13 '11 12:07

HomeBrew


People also ask

How do I convert UNIX time to normal time?

Simply multiply Unix timestamp by 1000 to convert it to a JavaScript time, because Unix timestamp measures time as a number of seconds, whereas in JavaScript time is fundamentally specified as the number of milliseconds (elapsed since January 1, 1970 at 00:00:00 UTC).

What format is Unix timestamp?

Unix epoch timestamps are supported in the following formats: 10 digit epoch time format surrounded by brackets (or followed by a comma). The digits must be at the very start of the message. For example, [1234567890] or [1234567890, other] followed by the rest of the message.

How do you convert date to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

Is Unix timestamp in seconds or milliseconds?

Unix is an operating system originally developed in the 1960s. Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC.


2 Answers

If you are using php you might want to try the following function which was posted by Matt Jones

http://www.mdj.us/web-development/php-programming/another-variation-on-the-time-ago-php-function-use-mysqls-datetime-field-type/

// DISPLAYS COMMENT POST TIME AS "1 year, 1 week ago" or "5 minutes, 7 seconds ago", etc...
function time_ago($date,$granularity=2) {
    $date = strtotime($date);
    $difference = time() - $date;
    $periods = array('decade' => 315360000,
        'year' => 31536000,
        'month' => 2628000,
        'week' => 604800, 
        'day' => 86400,
        'hour' => 3600,
        'minute' => 60,
        'second' => 1);

    foreach ($periods as $key => $value) {
        if ($difference >= $value) {
            $time = floor($difference/$value);
            $difference %= $value;
            $retval .= ($retval ? ' ' : '').$time.' ';
            $retval .= (($time > 1) ? $key.'s' : $key);
            $granularity--;
        }
        if ($granularity == '0') { break; }
    }
    return ' posted '.$retval.' ago';      
}
like image 74
calumbrodie Avatar answered Oct 12 '22 03:10

calumbrodie


I also like the jquery timeago plugin which will automatically update all time fields on a set timer so it is up to date if the user stays on a page for a while. You would need to convert the unix time to ISO 8601 format when rendering but I believe there is a php function for that.

like image 22
bkaid Avatar answered Oct 12 '22 02:10

bkaid