Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    mysql datetime reformat function

    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

    PHP Code:
    // 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
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,855
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Just use date_format()
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    function mysql2uk$date )
    {
        return 
    date'd-m-Y H:i:s'strtotime$date ) );

    Last edited by kbluhm; 08-07-2008 at 04:14 PM.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts
    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •