The ConvertDateTime() function takes a date and time string in the typical MySQL format (YYYY-MM-DD HH:MM:SS) and formats it as instructed by the formatting string.
1. <?php
2. // $datetime should contain the date and time in the 2007-09-19 14:08:06 format (common in MySQL)
3. // $format should contain the desired date and time format, as described at php.net/date
4. function ConvertDateTime($datestamp, $format)
5. {
6. if($datestamp != 0)
7. {
8. // Splits the date in multiple variables
9. list($date, $time) = split(" ", $datestamp);
10. list($year, $month, $day) = split("-", $date);
11. list($hour, $minute, $second) = split(":", $time);
12. // Gets the unix time by passing the variables we just initialized
13. $stampeddate = mktime($hour,$minute,$second,$month,$day,$year);
14. // Creates the new date and formats it according to the formatting strings
15. $datestamp= date($format,$stampeddate);
16. // Returns the formatted date
17. return $datestamp;
18. }
19. }
20.
21. echo ConvertDateTime("2007-09-19 14:08:06", "M d Y - H:m");
22. ?>