...

mysql datetime reformat function

hinch
08-07-2008, 01:02 PM
This isn't very big but I know alot of people around the place have looked for it similar (a quick google search proves this).

Basically its a simple function to take the mysql datetime field value and convert it into something a little more readable.


accepts date in format yyyy-mm-dd hh:mm:ss returns date in format dd-mm-yyyy hh:mm:ss


// convert dates FROM mysql format to UK format
function mysql2uk($date) {
$mainsplit = explode(" ",$date);
//$mainsplit[0]=date $mainsplit[1]=time
$datesplit = explode("-",$mainsplit[0]);
$newdate=$datesplit[2]."-".$datesplit[1]."-".$datesplit[0]." ".$mainsplit[1];
return $newdate;
}


Call it by using eg:

echo mysql2uk($date);



I know alot of you long time coders may find this useful but i'm fairly sure someone will use it at some point

abduraooft
08-07-2008, 01:07 PM
Just use date_format() (http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format)

hinch
08-07-2008, 01:15 PM
thats for doing it in sql and there are certain circumstances where its not practical or indeed doable.

for example i'm currently working on a system which has a 60 column table and rather than define every variable at once whoever first wrote it (i'm just a maintainer) did a select * and references the columns by arrays $returnedarray[4] etc. for me to change the select would then require me to go through a dozen different pages redoing 60 column displays per page.

so something like this at times will come in handy for someone especially at a guess newbie's just learning who haven't quite got their heads around it all

kbluhm
08-07-2008, 03:45 PM
function mysql2uk( $date )
{
return date( 'd-m-Y H:i:s', strtotime( $date ) );
}

;)

idalatob
09-03-2008, 08:53 AM
I love seeing code snippets like this, this is exactly how you learn and master your craft. You take ages to make something. Only to find someone else do it for you in 2 lines of code ;-) kudos to you guys (Hinch & kbluhm)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum