SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Dates

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Dates

    Hey,

    If I have a date in this format in my mysql db:

    Y-m-d H:i:s

    Once it's pulled out and ready to be displayed on a page, how can I get it to convert that into a nicer, easier format to read ie:

    Sunday 12th December 5:52pm

    Thanks

    Neil

  2. #2
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use the date_format function

    Code PHP:
    echo date_format($date, 'yourvalues');

  3. #3
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I have just tried the following and get errors:

    PHP Code:
    <p><?php date_format($news['datetime'], 'l ts F Y'); ?></p>
    $news['datetime'] is a date in the format I posted in my OP.

  4. #4
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what are the errors you're getting?

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Error is:

    Warning: date_format() expects parameter 1 to be DateTime, string given in D:\Files\wamp\www\neilnews\show_news.php on line 14

    Line 14 is:

    PHP Code:
    <h3><?php echo $news['subject']; ?></h3><p><?php echo $news['article']; ?></p><p><?php date_format($news['datetime'], 'l ts F Y'); ?></p>

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $date 
    '2010-12-25 00:00:00';
    echo 
    date('l jS F Y'strtotime($date));
    #Saturday 25th December 2010
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's great. Can you just explain how it is doing what it does?

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Sure, strtotime converts the string $date into a UNIX timestamp and then date formats said timestamp.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Provided the date is coming off a mysql date or datetime field it is faster to use MySQL's dateformat() function than use strtotime/date. Example:

    Code sql:
    SELECT DATE_FORMAT(datefield, '%W %D %M %l:%i%p') AS formattedDate FROM myTable

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,214
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Provided the date is coming off a mysql date or datetime field it is faster to use MySQL's dateformat() function than use strtotime/date.
    faster in what way?

    in performance? i remain unconvinced until i see actual and reliable cpu timings

    to code? again, i doubt it, as a good programmer can do it either way real fast

    the real test, in my opinion, is in maintainability

    most experienced develoipers will tell you that the job of the database is retrieval, and formatting should be done in the front end application

    this becomes painfully clear if you've ever worked in an organization where changes to working queries have to be vetted and approved by a separate part of the systems department, whereas changes to the app proceed without this bottleneck

    (at least, that's what i used to tell developers when i worked in the bottleneck, er, i mean, database section)

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •