Posters of Melbourne

When it comes to loving your city, I’m about as proud of Melbourne as you can get! So of course I would want to decorate the walls of my city apartment with this awesome Typographic Poster of Melbourne!

It’s a part of a series of City Posters from a good friend of mine and has other great prints from around the world including England and London, and they really are a great show piece. We get loads of comments on ours and make a great gift.

Do yourself a favour and buy yours today (or tomorrow, I guess)!

Month Dropdown in PHP

A simple way to generate an HTML select menu using PHP that lists the previous 12 months (or coming months).
You can easily change the output of the labels and option values by adjusting the output format from the calls to date().

<select name="month">
<?php
  for ($i = 0; $i <= 12; ++$i) {
    $time = strtotime(sprintf('-%d months', $i));
    $value = date('Y-m', $time);
    $label = date('F Y', $time);
    printf('<option value="%s">%s</option>', $value, $label);
  }
  ?>
</select>

capturing + (plus key) with keymaster.js

We use keymaster.js to power the keyboard shortcuts on a new product under development at work. It’s a great little javascript library for detecting key-presses from users.

For this product we wanted to capture the “+” key for when users wanted to Add something to their console. The confusion for me came from the fact the the “+” is used in the key() method for joining sequences of keys to get multi-key dispatching (e.g. pressing Control and Return at the same time).

It’s a pretty simple solution, but I couldn’t find it anywhere so thought I’d post here just in case someone else wants to do the same thing. As it turns out, “=” and “+” share the same key code in javascript, so the following will allow capturing of the plus key from both the numeric keypad as well as on the standard keyboard.

<script type="text/javascript" src="keymaster.min.js"></script>
<script type="text/javascript">
(function() {
  var myFunc = function() {
    console.log("User hit the + key");
  };
  key('=,shift+=', myFunc);
}());
</script>

An unfortunate by-product of the above, however, is that the same function will fire if the user hits just the “=” key. A pretty trivial trade-off IMHO. If anyone can think of a way to differentiate between the two let me know!

Use PHP sprintf to pad leading zeros

A simple way to use sprintf in PHP to pad a number with leading zeros.

<?php
 
// Pad the number 2 with zeros to get a 5 char (total) length string
$n = 2;
$n_padded = sprintf('%05d', $n);
 
// EXAMPLES
 
// Pad month/year to 2 digits in credit card form dropdown
$year_from = (int)date("y");
$year_to = $year_from + 6;
for($i = $year_from; $i <= $year_to; $i++) {
	$year = sprintf('20%02d', $i);
	echo "<option value="{$i}">{$year}</option>";
}
 
// Making up unique (Australian) mobile numbers for testing
$n = 0;
do {
	$mobile = sprintf('04%08d', $n++);
} while(is_existing_mobile($mobile));
 
?>