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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access