O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Retrieving Yesterday’s Metrics

Problem

I need to retrieve some of my metrics from yesterday.

Solution

Since the date field is in epoch seconds, you need to do a conversion before you can query on it:

<?php
$hoursAhead = 3;
$period = 86400; // 86400 = one day, 604800 = one week, 2592000 = one month

$yesterday = mktime($hoursAhead,0,0,date("m"),date("d")-1,date("Y"));
$monthAgo = mktime($hoursAhead,0,0,date("m"),date("d")-30,date("Y"));
$query = 'SELECT api_calls from metrics WHERE end_time = ' .
$yesterday . ' AND period = ' . $period . ';';
$metrics = $facebook->api_client->fql_query($query);
?>

You’ll need to substitute the number of hours that your server’s time zone is ahead of PST into the first line, and also change the fields you want to retrieve in the actual query if you want something other than just the number of API calls.

Discussion

The hours ahead of PST is important because Facebook calculates metrics at midnight PST and considers a day from midnight to 11:59:59 p.m. PST. You should always set your date ranges based on that time zone. See Formatting Relative Time for more information about epoch seconds.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required