2

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.

Community
  • 1
  • 1
bhavs
  • 2,091
  • 8
  • 36
  • 66
  • can you get the users timezone from the user connection https://developers.facebook.com/docs/reference/api/user/ and work it out from that? – TommyBs Jan 15 '13 at 16:29
  • @TommyBs I just looked at api and timezone information is only for the current user – bhavs Jan 15 '13 at 16:56
  • If you haven't got a satisfactory answer yet could you maybe clarify what you are looking for? – Bartek Jan 17 '13 at 11:03
  • @Bartek- I am figuring how the php code would blow would work, maybe once I have figured that out I will update the question with more clarifications or accept the answer – bhavs Jan 17 '13 at 12:46
  • @bhavs Just edited my answer which should help you but make sure that next time you just ask multiple questions separately. – Bartek Jan 17 '13 at 18:07
  • closing as a duplicate of [this ][1] [1]: http://stackoverflow.com/questions/3349293/relative-time-function-in-js-for-format-yyyy-mm-ddthhmmss0000 – bhavs Jan 18 '13 at 16:53

2 Answers2

4

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.

Bartek
  • 1,059
  • 6
  • 18
1

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.

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • @Shail thank you for this php code but unfortunately I am fetching this information in the JSON format and processing in java, hence I will map this php code to java code – bhavs Jan 15 '13 at 16:54
  • Not a big issu, you can still use this fnction :) – Sahil Mittal Jan 15 '13 at 17:26
  • I was looking at porting you r code to java, but I cannot figure out what $value is ? – bhavs Jan 17 '13 at 14:17
  • `$periods[$key] = $value` You may refer [here](http://php.net/manual/en/control-structures.foreach.php) – Sahil Mittal Jan 18 '13 at 05:39