php

Determine state from postcode in Australia

I had a little dilemma where I was importing a list of stores, but a required field was state. I had the postcodes for all these stores so I figured I could determine the state. I’ve dealt with postcode data before but I really couldn’t be bothered downloading that database from Australia Post (it’s pretty big). I’ve always known that postcodes starting with 2 are in NSW, 3 are in VIC, 4 are in QLD etc. so I set out to find what exactly determines the state from postcode.

The result was this article http://en.wikipedia.org/wiki/Postcodes_in_Australia#States_and_territories

And the PHP function is:

	/**
	/**
	* Returns the state for a postcode.
	* eg. NSW
	* 
	* @link http://en.wikipedia.org/wiki/Postcodes_in_Australia#States_and_territories
	*/
	function findState($postcode) {
		$ranges = array(
			'NSW' => array(
				1000, 1999,
				2000, 2599,
				2619, 2898,
				2921, 2999
			),
			'ACT' => array(
				200, 299,
				2600, 2618,
				2900, 2920
			),
			'VIC' => array(
				3000, 3999,
				8000, 8999
			),
			'QLD' => array(
				4000, 4999,
				9000, 9999
			),
			'SA' => array(
				5000, 5999
			),
			'WA' => array(
				6000, 6797,
				6800, 6999
			),
			'TAS' => array(
				7000, 7999
			),
			'NT' => array(
				800, 999
			)
		);
		$exceptions = array(
			872 => 'NT',
			2540 => 'NSW',
			2611 => 'ACT',
			2620 => 'NSW',
			3500 => 'VIC',
			3585 => 'VIC',
			3586 => 'VIC',
			3644 => 'VIC',
			3707 => 'VIC',
			2899 => 'NSW',
			6798 => 'WA',
			6799 => 'WA',
			7151 => 'TAS'
		);
 
		$postcode = intval($postcode);
		if ( array_key_exists($postcode, $exceptions) ) {
			return $exceptions[$postcode];
		}
 
		foreach ($ranges as $state => $range)
		{
			$c = count($range);
			for ($i = 0; $i < $c; $i+=2) {
				$min = $range[$i];
				$max = $range[$i+1];
				if ( $postcode >= $min && $postcode <= $max ) {
					return $state;
				}
			}
		}
 
		return null;
	}

ACR World

The ACR World website is a recruitment website and it wanted to upgrade the usablity and features of their site. I helped along with the process and even suggested a few features which they opted in for. The site is working well to this day.

http://www.acrworld.com