Current Time - @currenttime on Twitter

A few weeks ago, I was thinking to myself: "It's hard to tell what time certain tweets in my (already busy) timeline were posted... It'd be great if, instead of '18 minutes ago,' I could see that the post fell between 10 p.m. and 10:10 p.m. – it's much easier for me to remember tweets by time than by 'time ago.'

So, instead of trying to find some Twitter app that would let me insert my own timestamps or change the date format (I'm sure there are a few), I wrote a PHP script that would post a new tweet with the time in four time zones to the @currenttime Twitter account. Then, I followed @currenttime, and history was made ;-)

Here's the script I used (with help from Morethanseven's article, "Posting to Twitter using PHP"):

<?php
// Turn on error reporting for debug purposes

 
ini_set("display_errors","2");
 
ERROR_REPORTING(E_ALL);

/**
  * @file
  * Update Twitter status for 'Current Time' account
  *
  * @description
  * You can either run this script by visiting it in your browser, or by
  * setting up a cron job that uses wget or some similar method to grab
  * the script and run it at an interval you define.
  */

function post_tweet() {

 
// 1 - Define timestamps in various time zones
  //     For each time zone, you must set the default
  //     Use the date() function; see phpDocs for more info
 
     
$format = 'g:ia'; // Set time format to display as X:XXpm or X:XXam
 
     
date_default_timezone_set('America/Los_Angeles'); // Set default to Pacific
   
$pstTime = date("$format") . ' Pacific';

     
date_default_timezone_set('US/Mountain'); // Set default to Mountain
   
$mntTime = date("$format") . ' Mountain';
   
   
date_default_timezone_set('America/Chicago'); // Set default to Central
   
$cstTime = date("$format") . ' Central';
   
   
date_default_timezone_set('America/New_York'); // Set default to Eastern
   
$estTime = date("$format") . ' Eastern';

 
// 2 - Put timestamps together into a string for the Twitter status update

   
$tweetStatus = "$pstTime$mntTime$cstTime$estTime";

 
// 3 - Print the status to the screen

   
echo $tweetStatus;
    echo
"<br />\n";

   
// 4 - Post the status to Twitter
    // from: http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/
   
        // Set username and password
           
$username = 'usernamehere';
           
$password = 'passwordhere';
   
       
// The message you want to send
           
$message = $tweetStatus;
   
       
// The twitter API address
           
$url = 'http://twitter.com/statuses/update.xml';
           
       
// Alternative JSON version
            // $url = 'http://twitter.com/statuses/update.json';

        // Set up and execute the curl process
           
$curl_handle = curl_init();
           
curl_setopt($curl_handle, CURLOPT_URL, "$url");
           
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
           
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
           
curl_setopt($curl_handle, CURLOPT_POST, 1);
           
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
           
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
           
$buffer = curl_exec($curl_handle);
           
curl_close($curl_handle);

       
// check for success or failure - only seen if running in the browser
           
if (empty($buffer)) {
                echo
'<strong>Sorry, but it didn\'t work this time.</strong>';
            } else {
                echo
'<strong>This tweet was successfully posted to Twitter!</strong>';
            }

}

// Run the script

post_tweet();
?>

After saving that file, I simply set up a cron job to use wget and retrieve the script every 10 minutes. I hope to do more work with Twitter / PHP in the future :-)