Converting From MySQL DateTime to another Format in PHP
Submitted by nurhodelta_17 on Thursday, August 24, 2017 - 15:10.
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.- 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 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.- INSERT INTO `date` (`date_time`) VALUES
- ( '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.- <?php
- //MySQLi Procedural
- if (!$conn) {
- }
- //MySQLi Object-oriented
- //$conn = new mysqli("localhost","root","","date_format");
- //if ($conn->connect_error) {
- // die("Connection failed: " . $conn->connect_error);
- //}
- ?>
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".- <!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
- ?>
- <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
- ?>
- </body>
- </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
- 250 views