A function that formats date and time

A function that formats date and time
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. }
21. echo ConvertDateTime("2007-09-19 14:08:06", "M d Y - H:m");
22. ?>
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top