[Snippet] Simplified UK/US Date Conversion

One of my major annoyances in PHP & MySQL world is the different formats when working with dates.

US format: mm-dd-yy, UK format: dd-mm-yy and MySQL’s date,datetime, timestamp columns yy-mm-dd. How do you cope with them all? I’ve been working on a rich UI recently which includes the jQuery date picker, by default thats in US format. UK users expect a date in UK format.That was easily modified with

[codesyntax lang=”javascript”]
$( “#date_to” ).datepicker({ dateFormat: ‘dd-mm-yy’ });

UK and US dealt with.

So, what was my simplest way of converting that into MySQL format.

list ($d, $m, $y) = split ("-", $_GET['date_to']);
$date_to_mysql = $y."-".$m."-".$d;
$date_to = mysql_real_escape_string($_GET['date_to']);

We split the date given to us and chuck it back together. We should really put some security round the $_GET but you get the jist.

The last line is there just to repopulate the jQuery date picker when the page reloads.

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *