简体   繁体   English

使用FQL仅从Facebook API检索即将到来的生日

[英]Using FQL to retrieve only upcoming birthdays from Facebook API

Using the information on FQL and the user table from Facebook's Developer's docs, I've come up with the following code. 使用FQL信息和Facebook开发人员文档中的用户表 ,我提出了以下代码。

    //build fql string
    $fql = "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,3) 
            IN ('{$currentMonth}/', '{$nextMonth}/')";
    $fql.=$orderBy;
    $fql.=" LIMIT " . $limit;

    //query facebook
    $params  =   array(
                 'method'    => 'fql.query',       
                 'query'     => $fql,
                 'callback'  => ''     
                  );
    $birthdays  =  $this->facebook_obj->api($params);

$currentMonth and $nextMonth are set to string representations of the current month and next month respectively, in mm format. $currentMonth$nextMonth分别设置为当前月份和下个月的字符串表示形式,格式为mm。 (eg "09", "10" etc.) (例如“09”,“10”等)

This is "working", but let's say that today is November 29th, I have 20 friends with birthday's in November and $limit is set to 10. In this case, there is a very good chance that the 10 birthdays it returns will all be before November 29th. 这是“工作”,但是我们说今天是11月29日,我有11个朋友在11月生日, $limit设置为10.在这种情况下,很有可能它返回的10个生日都将是在11月29日之前。

What I would really like to do is get upcoming birthdays from today's day out, for this month and next. 我真正想做的是从今天开始,即本月和下一个即将到来的生日。 How could I modify this query to accomplish that? 我怎么能修改这个查询来实现呢?

Thanks! 谢谢!

* * Edit, see comment thread between Shawn and I for possible alternative solutions. * *编辑,请参阅Shawn和I之间的评论主题以寻找可能的替代解决方案。 No answer accepted yet as none directly addresses original code / question. 由于没有人直接解决原始代码/问题,因此尚未接受答案。

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. 基本上,FQL IN运算符不会检查该值是否在给定范围内,而是检查给定值是否与提供的任何不同值匹配。 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. 在您的示例中,如果值为“09”和“10”,则返回9月和10月的所有生日,因为这些生日月份与提供的月份数相匹配。

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): 您可以做的是使用PHP或您正在使用的任何其他语言计算以下值,并在您的FQL中使用它们(今天是3月17日,并说我们希望4月17日是结束日期):

  • Today's day of the month (17), 今天的今天(17),
  • Today's month of the year (03) 今年的今年(03)
  • End date day of the month (17) 截止日期(17)
  • End date month of the year (04) 截止日期(04)

Make sure days and months are formatted using dd and MM respectively (padded with zero in case of single digits). 确保分别使用dd和MM格式化日期和月份(在单个数字的情况下填充为零)。 Then your FQL would look like this: 然后你的FQL看起来像这样:

      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. 然后,这将返回3月17日至4月17日之间生日的朋友。

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

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

using "now" would return the current daytime. 使用“now”将返回当前的白天。


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: 你也可以让FQL为你做一些工作,如下所示:

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). 返回一个布尔类型“anon”字段,告诉您是否必须将此结果包含在显示列表中(true),或者此用户的生日是否已经过去(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. 不幸的是,我无法将这个命题作为一个WHERE子句 - 不断得到错误,但是很想知道是否有人比我更幸运。

  "error": {
    "message": "Call to a member function on a non-object", 
    "type": "BadMethodCallException"
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM