In this tutorial, I’m going to show you how to convert MySQL DateTime format into your desired size. I’ve added symbols and their value for you to understand the conversion further. This tutorial uses two mysqli methods that I have included in the comments. So, feel free to switch between the two.
Creating a 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.
CREATE TABLE `date` ( `dateid` INT(11) NOT NULL AUTO_INCREMENT, `date_time` datetime NOT NULL, PRIMARY KEY(`dateid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Inserting Data Into a 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.
INSERT INTO `date` (`date_time`) VALUES ( '2017-08-01 15:30:02');
Creating a 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.
<?php //MySQLi Procedural $conn = mysqli_connect("localhost","root","","date_format"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } //MySQLi Object-oriented //$conn = new mysqli("localhost","root","","date_format"); //if ($conn->connect_error) { // die("Connection failed: " . $conn->connect_error); //} ?>
Creating a 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”.
<!DOCTYPE html> <html> <head> <title>Converting From MySQL DateTime to another Format in PHP</title> </head> <body> <?php include('conn.php'); //MySQLi Object-oriented //$query = $conn->query("select * from `date` where dateid='1'"); //$row = $query->fetch_assoc(); //MySQLi Procedural $query=mysqli_query($conn,"select * from `date` where dateid='1'"); $row=mysqli_fetch_assoc($query); ?> <h2>Our Date: <?php echo $row['date_time']; ?></h2> Some Symbols to Consider: <ul> <li>Y : returns a four-digit year</li> <li>y : returns a two-digit year</li> <li>M : returns the first three letters of a month</li> <li>m : returns two digit numerical representation of a month</li> <li>F : Full letter representation of a month</li> <li>D : returns the first three letters of a day in a week</li> <li>d : returns two digit numerical representation of a day</li> <li>H : returns 24-hour clock</li> <li>h : returns 12-hour clock</li> <li>i : number of minutes</li> <li>s : number of seconds</li> <li>A : add AM/PM</li> </ul> <h2>Example Convertions:</h2> <?php //MySQLi Object-oriented //$date = new DateTime($row['date_time']); //echo $date->format('Y-m-d H:i:s')."<br>"; //echo $date->format('y-M-d h:i:s')."<br>"; //echo $date->format('F d, Y h:i A - D'); //MySQLi Procedural $date = date_create($row['date_time']); echo date_format($date, 'Y-m-d H:i:s')."<br>"; echo date_format($date, 'y-M-d h:i:s')."<br>"; echo date_format($date, 'F d, Y h:i A - D'); ?> </body> </html>
Happy Coding!