Using FQL to retrieve only upcoming birthdays from Facebook API

回眸只為那壹抹淺笑 提交于 2019-12-03 20:47:30

This is some months late but maybe helps other people facing the same problem. Basically, the FQL IN operator does not check whether the value is in a given range, but whether the given value matches any of the distinct values that are provided. In your example if '09' and '10' are the values, then all birthdays in September and October are returned since those birthday months match the provided month numbers.

What you can do is calculate the following values using PHP or whatever other language you are using and use them in your FQL (today is 17 March and say we want 17 April is end date):

  • Today's day of the month (17),
  • Today's month of the year (03)
  • End date day of the month (17)
  • End date month of the year (04)

Make sure days and months are formatted using dd and MM respectively (padded with zero in case of single digits). Then your FQL would look like this:

      SELECT name, birthday_date
      FROM user 
      WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
      AND strlen(birthday_date) != 0
      AND ((substr(birthday_date, 0, 2) = '03'
      AND substr(birthday_date, 3, 5) >= '17')
      OR (substr(birthday_date, 0, 2) = '04'
      AND substr(birthday_date, 3, 5) < '17'))
      ORDER BY birthday_date

This will then return the friends who's birthday falls between 17 March and 17 April.

AND created_time > strtotime('now') AND created_time < $endofnext

refer to samples @ http://www.php.net/manual/en/function.strtotime.php#97025

using "now" would return the current daytime.


This function will return the last day of next month no matter what month it is.

<?php 
/* $now can be set to any strtotime(), yesterday, last month, last year etc; */
$now = "now"; 
function EndOfNext($now){
    function lastDayOfMonth($month = '', $year = '') 
    { 
       if (empty($month)) { 
          $month = date('m'); 
       } 
       if (empty($year)) { 
          $year = date('Y'); 
       } 
       $result = strtotime("{$year}-{$month}-01"); 
       $result = strtotime('-1 second', strtotime('+1 month', $result)); 
       return date('m/d/y', $result); 
    } 
    function addRealMonth($timeStamp) 
    { 
        $tempMonth = date('m', $timeStamp); 
        $tempYear  = date('Y', $timeStamp); 
        if($tempMonth == "12") 
        { 
            $tempMonth = 1; 
            $tempYear++; 
        } 
        else 
            $tempMonth++; 

        $newDate = lastDayOfMonth($tempMonth, $tempYear); 
        return strtotime($newDate); 
    }
    $newChargeDate = strtotime($now); 
    $newChargeDate = addRealMonth($newChargeDate); 
    return date("m/d/y", $newChargeDate); 
}
$endofnext = EndOfNext($now);
echo $endofnext;
?>

You can also let FQL do some job for you, like in the following:

SELECT uid, name, birthday_date, strtotime(substr(birthday_date, 0, 5)) > now()
FROM user
WHERE uid in (SELECT uid2 FROM #freunde)
AND birthday_date <> ''
AND strpos(birthday_date,'06') = 0
ORDER BY substr(birthday_date, 0, 5)

returning a boolean type "anon" field telling if you have to include this result in the display list (true) or if the birthday for this user lies already in the past (false). Unfortunately I wasn't able to have this proposition work as a WHERE clause - keep getting the error under, but would love to know if someone was luckier than I.

  "error": {
    "message": "Call to a member function on a non-object", 
    "type": "BadMethodCallException"
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!