Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Return the current tweet count for todays date
<?php
function get_todays_tweet_count($user_id, $cache_for=900) {
if (false !== ($count = get_transient('hl_todays_tweet_count'))) {
return (int) $count;
}
$response = wp_remote_get('http://twitter.com/statuses/user_timeline/'.absint($user_id).'.json');
if(is_wp_error($response)) return false;
$json = $response['body'];
if(!$json) return false;
$tweets = json_decode($json);
if(!$tweets) return false;
$today = date_i18n('D M d'); # compare strings rather than parse to timestamp
$count = 0;
foreach($tweets as $tweet) {
if($today != substr($tweet->created_at, 0, 10)) break; # stop loop as soon as not today
$count++;
}
set_transient('hl_todays_tweet_count', $count, absint($cache_for));
return (int) $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.