PHP - break apart date and rearrange depending on set variable

Posted on 2007-07-24
Last Modified: 2007-07-24
I'm still learning PHP.  And I figured out how to do this to a point... but I think there is a better way.
And I'm in a big hurry, so lots of points for what is somewhat simple.

I'm storing my dates in the format YYYYMMDD.
There is a variable that is set by the client.

I need to have PHP pull that date and convert it to:
Mar 6 1980 if a variable is set one way...  or
3 6 1980 if it's set another... or
03 06 1980 if it's set the other way.

Question by:innercproductions
    LVL 24

    Accepted Solution

    You can use strtotime() to convert your date into a format that PHP can use, use date() to format the output as needed, and use a switch statement to handle the variable:


    $date = '20070724';

    $dateType = rand(1,3); // I used rand() here for the example, but you will set this to the client selection.

    switch ($dateType)
          case 1:
                echo date('M j Y', strtotime($date));
          case 2:
                echo date('n j Y', strtotime($date));
          case 3:
                echo date('m d Y', strtotime($date));

    LVL 24

    Expert Comment

    For more information about formatting dates using the date() function, see the PHP manual page at .

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now