PHP - break apart date and rearrange depending on set variable

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.

Who is Participating?
glcumminsConnect With a Mentor Commented:
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));

For more information about formatting dates using the date() function, see the PHP manual page at .
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.