Fetching / formatting / storing tweets to my database I am periodically getting tweets, formatting them, and storing them into the database. Now the line of code I use for this is an awful lot. I was wondering if this could be improved.

// This is the wordpress way of getting a json object
// Wordpress will determine how the data is going to 
// be fetched looking at what PHP functions are 
// enabled for the user, wget, curl, etc.
$json_body = wp_remote_retrieve_body(
    wp_remote_get(
        'http://twitter.com/statuses/user_timeline/'.$options['username'].'.json?count='.$options['count'] ) );

$tweet = json_decode( $json_body, true );
for( $i = 0; $i &lt; $options['count']; $i++ ){
  $latestTweet = htmlentities($tweet[$i]['text'], ENT_QUOTES);
  $latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&amp;\!\#\~\/\,]+)/i', '&lt;a href="http://$1" target="_blank"&gt;http://$1&lt;/a&gt;', $latestTweet);
  $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '&lt;a href="http://twitter.com/$1" target="_blank"&gt;@$1&lt;/a&gt;', $latestTweet);

  // This array $data will be stored to the DB
  // I will use the *serialize()* function for this.
  $data[] =  array( 'text' =&gt; $latestTweet, 'time' =&gt; timespan( $tweet[$i]['created_at'] ) );
}

function timespan( $a )
{
  //get current timestampt
  $b = strtotime("now");
  //get timestamp when tweet created
  $c = strtotime($a);
  //get difference
  $d = $b - $c;
  //calculate different time values
  $minute = 60;
  $hour = $minute * 60;
  $day = $hour * 24;
  $week = $day * 7;

  if(is_numeric($d) &amp;&amp; $d &gt; 0) {
    //if less then 3 seconds
    if($d &lt; 3) return "right now";
    //if less then minute
    if($d &lt; $minute) return floor($d) . " seconds ago";
    //if less then 2 minutes
    if($d &lt; $minute * 2) return "about 1 minute ago";
    //if less then hour
    if($d &lt; $hour) return floor($d / $minute) . " minutes ago";
    //if less then 2 hours
    if($d &lt; $hour * 2) return "about 1 hour ago";
    //if less then day
    if($d &lt; $day) return floor($d / $hour) . " hours ago";
    //if more then day, but less then 2 days
    if($d &gt; $day &amp;&amp; $d &lt; $day * 2) return "yesterday";
    //if less then year
    if($d &lt; $day * 365) return floor($d / $day) . " days ago";
    //else return more than a year
    return "over a year ago";
  }
}
wp_remote_get(
        'http://twitter.com/statuses/user_timeline/' 
        . $options['username'].'.json?count='.$options['count'] ) 


I'd use at least a local variable which stores the url:

$url = 'http://twitter.com/statuses/user_timeline/' . 
    $options['username'] . '.json?count=' .$options['count'];
wp_remote_get($url);


A function would be more better. It makes the code easier to read.



Use longer variable names to avoid comments:

function timespan($create_time_input)
{
    $current_timestamp = strtotime("now");
    $create_time = strtotime($create_time_input);
    $diff_seconds = $current_timestamp - $create_time;

    $one_minute = 60;
    $one_hour = $one_minute * 60;
    $one_day = $one_hour * 24;
    $one_week = $day * 7;

    if(is_numeric($d) &amp;&amp; $d &gt; 0) {
        if ($diff_seconds &lt; 3) {
            return "right now";
        }
        if ($diff_seconds &lt; $one_minute) {
            return floor($diff_seconds) . " seconds ago";
        }
...


Now the code itself says what the comments said before.



Unnecessary floor:

if($d &lt; $minute) return floor($d) . " seconds ago";


$d looks an integer, so floor is unnecessary.