O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #19. Build a DHTML Binary Clock

Use a combination of PHP and DHTML to build the bit clock that you can buy on ThinkGeek.com.

A coworker of mine recently bought one of the "bit clocks" from ThinkGeek (http://www.thinkgeek.com/). The clock has three sections, each with eight lights. The sections represent seconds, minutes, and hours, and the individual lights represent the bits in each value. Though neither he nor I could actually read the clock (!!), we both agreed that it was very cool. Along the same lines, here's a hard-to-read clock in PHP, also super-cool to show off.

The Code

clock.php does the work; it's shown in Example 3-12.

Example 3-12. Setting up an array of bit masks, and beyond

<?php $bit_names = array( 1, 2, 4, 8, 10, 20 ); $bit_masks = array( array( '0', '0x1', '1' ), array( '1', '0x2', '2' ), array( '2', '0x4', '4' ), array( '3', '0x8', '8' ), array( '4', '0x10', '16' ), array( '5', '0x20', '32' ) ); $size = 40; function bit_table( $name ) { global $size; ?> <table width="<?php echo($size*2); ?>" cellspacing="2" cellpadding="0"> <tr> <td id="<?php echo( $name ); ?>_1" width="<?php echo($size); ?>" height="<?php echo($size); ?>" /></td> <td id="<?php echo( $name ); ?>_2" width="<?php echo($size); ?>" height="<?php echo($size); ?>" /></td> </tr> <tr> <td id="<?php echo( $name ); ?>_4" width="<?php echo($size); ?>" height="<?php echo($size); ?>" /></td> <td id="<?php echo( $name ); ?>_8" width="<?php echo($size); ?>" height="<?php echo($size); ?>" /></td> </tr> <tr> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required