SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Posts
    245
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Date format?

    When you storing DATE in MySQL, the default DATE datatype format is YYYY-MM-DD

    For example: 2006-05-20

    How to convert this to '20 May 2006' in PHP?
    [Home Sweet Home]

  2. #2
    SitePoint Member supremewebdesign's Avatar
    Join Date
    Apr 2009
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // where $date is the date you wish to convert

    // first convert to a timestamp
    list($year$month$day) = explode('-'$date);

    $timestamp mktime(000$month$day$year);

    // then convert to a date
    $converted date('j F Y'$timestamp); 
    http://uk2.php.net/manual/en/function.explode.php - php explode function
    http://www.php.net/manual/en/function.mktime.php - php mktime function
    http://www.php.net/manual/en/function.date.php - php date function

    Hope this helps!
    www.supremewebsolutions.com
    Aberdeen based web design.
    Hotel booking systems - Content management systems
    CMS - Search engine optimisation - SEO

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also do it right when it comes out of MySQL using date_format:

    Code MySQL:
    SELECT 
        date_format(field_name, '%d %M %Y'), 
        other_field_name,
        maybe_another_field
    FROM 
        table_name

  4. #4
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo date('d M Y'); 
    For mysql:
    PHP Code:
    $date $row->date;
    echo 
    date('d M Y',strtotime($date)); 

  5. #5
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check here: us2.php.net/manual/en/function.strtotime.php
    (there are many date/time related functions in PHP - you should surf around at php.net)

    Or, if you intend to do math or otherwise manipulate the m/d/y pieces of the date, try this:
    list($y, $m, $d) = explode($StoredDate, "-");

    [sorry for duplicating the answers of others - I type too slow, I guess.]
    Last edited by eLIANT; Oct 10, 2009 at 11:10. Reason: Too slow...

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    To decide where to do the conversion to the date format you want.

    If it is a really simple application where you are extracting the date and displaying it straight away then use the SQL command to return the date in the format you are going to display it in.

    If the application is more involved then process the date throughout in the ccyy-mm-dd format that it gets returned in and have convert it to the desired display format when processing the template file to output the page.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a huge detailed list in PHP.net, check that list. You can provide any date format with true codes.


Bookmarks

Posting Permissions

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