Converting From MySQL DateTime to another Format in PHP

Language
In this tutorial, Im going to show you how to convert mysql datetime format into your desired format. I've added symbols and their value for you to further understand the conversion. This tutorial uses two mysqli methods that I have included in the comments. So, feel free to switch between the two.

Creating our Database

First, we're going to create a database that contains our data. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as "date_format". 3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `date` (
  2.   `dateid` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `date_time` datetime NOT NULL,
  4. PRIMARY KEY(`dateid`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
date

Inserting Data Into our Database

Next Step in to insert some data into our database. This data contain our sample date. 1. Click the database "date_format" that we have created earlier. 2. Click SQL and paste the code below.
  1. INSERT INTO `date` (`date_time`) VALUES
  2. ( '2017-08-01 15:30:02');

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
  1. <?php
  2.  
  3. //MySQLi Procedural
  4. $conn = mysqli_connect("localhost","root","","date_format");
  5. if (!$conn) {
  6.  die("Connection failed: " . mysqli_connect_error());
  7. }
  8.  
  9. //MySQLi Object-oriented
  10. //$conn = new mysqli("localhost","root","","date_format");
  11. //if ($conn->connect_error) {
  12. //    die("Connection failed: " . $conn->connect_error);
  13. //}
  14.  
  15. ?>

Creating our Show Page

Lastly, we create our show page. This page will show our conversions. To create the page, open your HTML code editor and paste the code below after the tag. We name this page as "index.php".
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Converting From MySQL DateTime to another Format in PHP</title>
  5. </head>
  6. <body>
  7.         <?php
  8.                 include('conn.php');
  9.                 //MySQLi Object-oriented
  10.                 //$query = $conn->query("select * from `date` where dateid='1'");
  11.                 //$row = $query->fetch_assoc();
  12.                
  13.                 //MySQLi Procedural
  14.                 $query=mysqli_query($conn,"select * from `date` where dateid='1'");
  15.                 $row=mysqli_fetch_assoc($query);
  16.         ?>
  17.         <h2>Our Date: <?php echo $row['date_time']; ?></h2>
  18.         Some Symbols to Consider:
  19.                 <ul>
  20.                         <li>Y : returns a four-digit year</li>
  21.                         <li>y : returns a two-digit year</li>
  22.                         <li>M : returns the first three letters of a month</li>
  23.                         <li>m : returns two digit numerical representation of a month</li>
  24.                         <li>F : Full letter representation of a month</li>
  25.                         <li>D : returns the first three letters of a day in a week</li>
  26.                         <li>d : returns two digit numerical representation of a day</li>
  27.                         <li>H : returns 24-hour clock</li>
  28.                         <li>h : returns 12-hour clock</li>
  29.                         <li>i : number of minutes</li>
  30.                         <li>s : number of seconds</li>
  31.                         <li>A : add AM/PM</li>
  32.                 </ul>  
  33.         <h2>Example Convertions:</h2>
  34.         <?php
  35.        
  36.         //MySQLi Object-oriented
  37.         //$date = new DateTime($row['date_time']);
  38.         //echo $date->format('Y-m-d H:i:s')."<br>";
  39.         //echo $date->format('y-M-d h:i:s')."<br>";
  40.         //echo $date->format('F d, Y h:i A - D');
  41.  
  42.         //MySQLi Procedural
  43.         $date = date_create($row['date_time']);
  44.         echo date_format($date, 'Y-m-d H:i:s')."<br>";
  45.         echo date_format($date, 'y-M-d h:i:s')."<br>";
  46.         echo date_format($date, 'F d, Y h:i A - D');
  47.        
  48.         ?>
  49. </body>
  50. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment