bhavs
bhavs

Reputation: 2301

time returned with facebook graph search for posts api

I am using facebook graph api to search for posts matching a particular query.But I cannot find any documentation about the time zone of the time stamp returned in the json format.

My query is this and the response the JSON arrray where each element has a timeStamp which looks like this : 2013-01-15T14:27:49+0000

How do I use this timeStamp ( without time zone information ) to display fb content on an external website:

 <snip>post content </snip> poster-profile image</snip>
   < snip> post created 5 minutes ago </snip> 

Edit : I am using the grails framework for the backend implementation. hence using php code to obtain timeStamp information would not be very easy

Edit 2 : I am using the search api to search over all publicly accessible posts, so I am not suing any access token.

Edit 3 : closing this question as a possible duplicate of this.

Upvotes: 2

Views: 1167

Answers (2)

Bartek
Bartek

Reputation: 1059

Have a look at the Dates in Facebook Docs and then ISO 8601 Time Zone.

In your example 2013-01-15T14:27:49+0000 the string means 14:27:49 UTC (because it is followed by +0000).

EDIT
Let me expand on my answer. Once you know the UTC time, getting the time difference which you want to display on your page is easy. You can either check what that time that UTC timestamp corresponds to in your timezone and then compare it to the current time OR check what UTC it is at the moment and calculate the difference between it and the timestamp.

If you do not know how to do that maybe have a look at the Pretty Time Grails plugin.

Upvotes: 4

Sahil Mittal
Sahil Mittal

Reputation: 20753

If you are using PHP, you may use this-

// 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 ' post created '.$retval.' ago';      
}

Reference.

Upvotes: 1

Related Questions