Lightweight PHP Code Timer

Sep
30

I wanted to profile some of my PHP code to benchmark it and also to help find some bottlenecks. I couldn't find an easy way to do this, so I decided to create my own timer. It is accurate down to microseconds, but milliseconds have been easier for me to work with.

<?php
class Timer {
   private $startTime;

   function __construct() { $this->startTime = microtime(true); }

   function reset() { $this->__construct(); }

   function __toString() {
      return "Elapsed " . (microtime(true) - $this->startTime) * 1000 . " milliseconds.\n";
   }
}

$timer = new Timer();
// code
echo $timer;
$timer->reset();
// more code
echo $timer;
?>

An example output from the code above would be as follows:

Elapsed 0.94294548034668 milliseconds.
Elapsed 3.9780139923096 milliseconds.

Facebook Fanpage Like Count Without Authentication

Sep
27

Heres is a simple way to fetch the number of likes for a Facebook fanpage without authentication. The below implementation is to be run server-side using PHP, but this could be easily implemented using JavaScript as well.

<?php
function fetchFacebookLikes($username) {
   $url = "http://api.facebook.com/method/fql.query?format=json&query="
        . urlencode("SELECT fan_count FROM page WHERE username='$username'");
   $result = json_decode(file_get_contents($url));
   return $result[0]->fan_count;
}
echo fetchFacebookLikes("checkiday");
?>

Another alternative would be to use page_id='$username' instead of username='$username' in the query and pass the function a page id: 151259271601760.

Get a User's Timezone using JavaScript/jQuery and PHP

Apr
11

I wrote a function using jQuery and PHP that I have tested.

On the PHP page where you are want to have the timezone as a variable, have this snippet of code somewhere near the top of the page:

<?php    
   session_start();
   $timezone = $_SESSION['time'];
?>

This will read the session variable "time", which we are now about to create.

On the same page, in the <head>, you need to first of all include jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">

Also in the <head>, below the jQuery, paste this:

<script type="text/javascript">
   $(function() {
      if (0 == "<?=$timezone?>".length) {
         var visitortime = new Date();
         var visitortimezone = "GMT " + -visitortime.getTimezoneOffset() / 60;
         $.ajax({
            type: "GET",
            url: "http://domain.com/timezone.php", // Change this!
            data: 'time='+ visitortimezone,
            success: function(){
               location.reload();
            }
         });
      }
   });
</script>

You may or may not have noticed, but you need to change the url to your actual domain.

One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this: (create a new file called **timezone.php** and point to it with the above url)

<?php
   session_start();
   $_SESSION['time'] = $_GET['time'];
?>

If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.