PHP Date format conversion

Discussion in 'PHP' started by Lashan, Sep 9, 2011.

  1. #1
    Hi guys, I have a date field in MySql database table to enter dates. In my php code I used long date style to enter date value. So the data in Date field of the table is in following format.

    08th of September 2011

    But now I want to change that date to short date format which should looks like this....

    08-09-2011

    To convert this I tried following code, but didn't work.

    $todayDate = "08th of September 2011";
    $new_date = date('d-m-Y', strtotime($todayDate));

    Can somebody help me to convert this long date to short date style in PHP please?
     
    Lashan, Sep 9, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    that strtotime should work but if its wasting you too much time and the format is consistent then explode it and parse it yourself.

    The database should be saving it as Y-m-d (big date measure, middle date measure, smallest date measure) which also makes sorting feasible.

    Personally I'd only recommend that verbose date formatting for presentation of data, never for input.

    If people are setting a date on a form they can always select September from a dropdown but the saved value would be 09, etc
     
    sarahk, Sep 9, 2011 IP
  3. CleverTechguy

    CleverTechguy Peon

    Messages:
    58
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can use strtotime or mktime function to format date in required format
     
    CleverTechguy, Sep 10, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Why not just link a calendar (jQuery has a nice one) to the input box so they can pick the date? That way you'll get a date formatted in way that it can be inserted into a database.
     
    Rukbat, Sep 10, 2011 IP
  5. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #5
    The "of" screws up strtotime.
    08th of September 2011
    08th September 2011 should work fine, though adds an annoyance in removing the word.


    I don't know if it's possible for you, but what I would advise is storing the date in the the timestamp format, instead of using the DATE type in MySQL.

    For example:

    
    <?php
    //Before data is added to the database.
    $time = '08th September 2011'; 
    $timeStamp = strtotime($time);
    
    #Store $timeStamp in your database field.
    #In this case, $timeStamp = 1315404000
    #When retrieving the date from your 
    #database, do this:
    
    $time = '1315404000'; //This has been retrieved from the date field.
    $formattedTime = date('d-m-Y', $time);
    ?>
    PHP:
    Hopefully this helps.
    Cheers
     
    blueparukia, Sep 11, 2011 IP