SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Format date in PHP

    Given the string "2013-12-29", what PHP code snippet will produce "December 29, 2013"?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,067
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $d = new \DateTime("2013-12-29");
    echo 
    $d->format('F d, Y'); 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Evangelist artcoder's Avatar
    Join Date
    Aug 2005
    Location
    Planet Earth
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have to admit. That's a good one. Works great.

  4. #4
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I know this is a solved issue but, I will make it complete.
    This will work for PHP >= 5.2.0

    A "classical" option is
    PHP Code:
    <?php
    echo date("F d, Y"strtotime('2013-12-29'));
    ?>
    Still, the OOP version is better, if PHP >= 5.2.
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vectorialpx View Post
    I know this is a solved issue but, I will make it complete.
    This will work for PHP >= 5.2.0

    A "classical" option is
    PHP Code:
    <?php
    echo date("F d, Y"strtotime('2013-12-29'));
    ?>
    Still, the OOP version is better, if PHP >= 5.2.
    Good suggestion. But better on not, I'd still go for the classical version even in newer php versions because it's faster . Of course, only when this simple conversion is all I want to do.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    Of course, only when this simple conversion is all I want to do.
    Of course most times you'd run the two statements of the OOP version at completely different times - the first when the data is retrieved from the database and the second at whichever spot you wanted to output it. They would usually be inside methods belonging to different objects.

    The classic option is only really appropriate to procedural coding.
    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="^$">


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
  •