Flexible UK Date Converter Function for PHP and MYSQL
South Coast Web Design Ltd
UK Date Mysql Convert Function Jan 09

source-php-icon

Just a quick float past today to share a function have i written and use daily that will convert mysql date format into UK formatted equivilent dates.
For mysql to work correctly on date searches etc. mysql stores dates in the ‘YYYY-MM-DD’ format
In the UK we like to read as ‘DD/MM/YYYY’ just to be different.
In the USA the dates are displayed as ‘MM/DD/YYYY’ I am lead to believe by my recent visit.

I am going to share my 2 functions for date conversion today that will take an UK inputted date and convert to a mysql date, and then the second function takes the date and converts it back into a UK formatted Date. Feel free to use these functions whenever you wish.

function changedate($date) {
   $chdate = explode('/',$date);
   $changedate = $chdate[2].'-'.$chdate[1].'-'.$chdate[0];
   return $changedate;
}

Just a quick run through:
we create an array called $chdate and we explode the result of the $date input using / (if you have used a – just change to that)..
That should give us an array of 3 strings. We then put that array back together in the correct mysql order for storing in the database.

Now to convert it back to a UK date profile i have reversed the function:

function ukdate($date) {
   $chukdate = explode('-',$date);
   $ukchdate = $chukdate[2].'/'.$chukdate[1].'/'.$chukdate[0];
   return $ukchdate;
}

It is nice and flexible and you can do a USA conversion as below:

function usadate($date) {
   $chusadate = explode('-',$date);
   $usachdate = $chusadate[1].'/'.$chusadate[2].'/'.$chusadate[0];
   return $usachdate;
}

To call the function on a sql date just do :

echo ukdate($row[date]);

So if you save these functions in your function file for future use and enjoy…

===========
Author Edit
===========
These 2 functions are really useful and i use them alot. However i have been developing a project that
uses timestamp in its database instead of date. After a few hours research and a bit of tickering i discovered
php actually has a built in function that you can pretty much manipulate into any form you like.

$sqldate = '2012-12-25';
$newdate = date('d/m/Y',strtotime($sqldate));
echo $newdate;

Will give you a UK based date (25/12/2012 in this case). You can use this with many of the date fetaures in pretty much any order.
You can also use this with a mysql timestamp as well. If you wish to add the time use :

$sqltimestamp = '2012-12-25 10:32:23';
$newdate = date('d/m/Y',strtotime($sqltimestamp));
$newtimestamp = date('d/m/Y H:i:s',strtotime($sqltimestamp));
echo $newdate.'
';
echo $newtimestamp;

Will give you:
25/12/2012
25/12/2013 10:32:23

I hope this also helps.

One Response to “UK Date Mysql Convert Function”

Tony LancasterSeptember 21st, 2013 at 12:43 pm

Very helpful – just as a fun fact ‘only’ 300 million Americans use mmddyy – about 3 billion of the rest of us do it the ‘right’ way!

Leave a Response


Top