开发者

return all the array between one hour via php

开发者 https://www.devze.com 2023-03-10 09:11 出处:网络
lets say we do some search useing search api then it retuns something like this in json. \'results\' =>

lets say we do some search useing search api then it retuns something like this in json.

  'results' => 
    array
      0 => 
        object(stdClass)[9]
          public 'from_user_id_str' => string '57393273' (length=8)
          public 'profile_image_url' => string 'http://a1.twimg.com/profile_images/1150792547/49146_1365401218_7951632_q_normal.jpg' (length=83)
          public 'created_at' => string 'Mon, 06 Jun 2011 04:01:36 +0000' (length=31)
          public 'from_user' => string 'primalokomotif' (length=14)
          public 'id_str' => string '77585906684526592' (length=17)
          public 'metadata' => 
            object(stdClass)[10]
              public 'result_type' => string 'recent' (length=6)
          public 'to_user_id' => null
          public 'text' => string 'RT @detikcom kober siap perkenalkan ajaran klub suami suami takut istri' (length=71)
          public 'id' => float 7.7585906684527E+16
          public 'from_user_id' => int 57393273
          public 'geo' => null
          public 'iso_language_code' => string 'id' (length=2)
          public 'to_user_id_str' => null
          public 'source' => string '<a href="http://mobile.twitter.com" rel="nofollow">Mobile Web</a>' (length=97)
      1 => 
        object(stdClass)[11]
          public 'from_user_id_str' => string '278060132' (length=9)
          public 'profile_image_url' => string 'http://a1.twimg.com/profile_images/1355377589/296218442_normal.jpg' (length=66)
          public 'created_at' => string 'Mon, 06 Jun 2011 04:01:34 +0000' (length=31)
          public 'from_user' => string 'kaka_kecil' (length=10)
          public 'id_str' => string '77585897637412864' (length=17)
          public 'metadata' => 
            object(stdClass)[12]
              public 'result_type' => string 'recent' (length=6)
          public 'to_user_id' => null
          p开发者_如何学编程ublic 'text' => string 'RT @detikcom: 'Milan Tak Pernah Inginkan Hamsik' http://de.tk/dP7Cs via @detiksport' (length=83)
          public 'id' => float 7.7585897637413E+16
          public 'from_user_id' => int 278060132
          public 'geo' => null
          public 'iso_language_code' => string 'id' (length=2)
          public 'to_user_id_str' => null
          public 'source' => string '<a href="http://ubersocial.com" rel="nofollow">ÃœberSocial</a>' (length=94)
      2 => 
        object(stdClass)[13]
          public 'from_user_id_str' => string '185984810' (length=9)
          public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1368466468/a_normal.jpg' (length=58)
          public 'created_at' => string 'Mon, 06 Jun 2011 04:01:31 +0000' (length=31)
          public 'from_user' => string 'tiayeeeaah' (length=10)
          public 'id_str' => string '77585887143276544' (length=17)
          public 'metadata' => 
            object(stdClass)[14]
              public 'result_type' => string 'recent' (length=6)
          public 'to_user_id' => null
          public 'text' => string 'hmm..hmm.. -___-" RT @detikcom: Bandung Siap Perkenalkan Ajaran Klub Istri Taat Suami http://bit.ly/kCcOsG' (length=111)
          public 'id' => float 7.7585887143277E+16
          public 'from_user_id' => int 185984810
          public 'geo' => null
          public 'iso_language_code' => string 'id' (length=2)
          public 'to_user_id_str' => null
          public 'source' => string '<a href="http://seesmic.com/seesmic_desktop/sd2" rel="nofollow">Seesmic Desktop</a>' (length=115)
      3 => etc

how can we return arrays that have date less then one hour ? or filter it? maybe something like do a search then returns all the array that have strtotime(created_at) < strtotime("+1 Hours")

*edit

like How can I use the Twitter Search API to return all tweets that match my search query, posted only within the last five seconds? but there is no answer doing this.

thanks for looking in

Adam Ramadhan


So, you're looking to return only those that have created_at within the past hour? I like to use array_filter() when I can.

function filterWithinHour($obj)
{
    return (strtotime($obj->created_at) > strtotime('-1 hour'));
}

$recent = array_filter($results, 'filterWithinHour');

If you ever need need to pass it a certain time (instead of assuming now), you might want to just loop through the array manually rather than mess with trying to pass a time parameter to the callback with a global variable. I hate globals used like that.


The Twitter Search API has a since_id that could help you:

Returns results with an ID greater than (that is, more recent than) the specified ID. There are limits to the number of Tweets which can be accessed through the API. If the limit of Tweets has occured since the since_id, the since_id will be forced to the oldest ID available.

If you poll every hour, you effectively get the posts of the last hour. If the service is down, you still won't miss any tweets since you could go back several days.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号