Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

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.

0
innercproductions
Asked:
innercproductions
  • 2
1 Solution
 
glcumminsCommented:
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:

<?php

$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));
            break;
      case 2:
            echo date('n j Y', strtotime($date));
            break;
      case 3:
      default:
            echo date('m d Y', strtotime($date));
            break;
}

?>
0
 
glcumminsCommented:
For more information about formatting dates using the date() function, see the PHP manual page at http://www.php.net/date .
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now