How to Convert a Date Format in PHP

PHP contains functions for printing out dates and times according to a variety of human readable formats that can be specified using format strings. A slightly more difficult problem is reading date strings in one format and outputting them into a different format. This is accomplished by combining the "date" and "strtotime" functions.

Instructions

    • 1

      Open a command prompt. In Windows click "Start," "Run" and type "cmd."

    • 2

      Type the following command to enter PHP interactive mode: php -a.

    • 3

      Type the following to input an English date format:

      $origDateString = "12 May 2010";

      $origDateInt = strtotime($origDateString);

    • 4

      Type the following to convert the date to a new format:

      echo date("l F d y", $origDateInt);

      This will convert the original date format to a new format that reads "Wednesday May 12 10."

Tips & Warnings

  • This procedure first parses the original date format and stores it in an integer that keeps track of the seconds since midnight January 1, 1970. Then it uses this to recreate the date string in the new, requested format.

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured