Update for Geocoding Google maps using Maps API V3

I’ve recently updated the geocoding system posted previously (many years ago) to work better with the latest google maps API (V3).  The previous post worked fine until fairly recently then something changes.  This new method is more OO based and relies on a class for geocoding which you can call at any point you like.

Much easier to use, less code, more reuseable and work 100% with latest google API 🙂


class geocoder{
        static private $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=";

        static public function getLocation($address){
            $url = self::$url.$address;

            $resp_json = self::curl_file_get_contents($url);
            $resp = json_decode($resp_json, true);

                return $resp['results'][0]['geometry']['location'];
                return false;

        static private function curl_file_get_contents($URL){
            $c = curl_init();
            curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($c, CURLOPT_URL, $URL);
            $contents = curl_exec($c);

            if ($contents) return $contents;
                else return FALSE;


Now to use it

include "system/geocode.class.php";
$address = urlencode(trim($incoming_address));
$loc = geocoder::getLocation($address);
echo "Lat: ".$loc->["lat"];
echo "<br />";
echo "Long: ".$loc->["lng"];

MySQL Date format and PHP

This is just a very quick blog entry to provide a little snippet of code and hopefully help some people.

I offer coding help and advice on several PHP forms for beginners to the language.  One of the common problems they have seems to be date/time stamps and MySQL format and handling.  This seems to be especially relevant to UK coders where our dates take the format dd-mm-yyyy whereas MySQL stores the date in yyyy-mm-dd HH:mm:ss.

PHP is a wonderful language that makes working with dates etc actually very easy.  To resolve the format of date/times from MySQL to a more UK centric format I created a very simple function

function mysql2uk($date) {
      return date('d-m-Y H:i:s',strtotime($date));
function mysql2uk($date) {
 return date('d-m-Y H:i:s',strtotime($date));
$sql ="select * from table";
$result = mysql_query($sql) or die();
$values = mysql_fetch_row($result);
echo mysql2uk($values['column']);
And as easy as that your date is now in a UK format.

You can edit the format by changing the ‘d-m-Y H:i:s” part of the function and to save you having to have the function at the top of everyone of your pages you can put it in an include file and just include that file on your site/pages where you want the function available.

Hope this little snippet helps some.