CREATE TABLE `date` ( `dateid` INT(11) NOT NULL AUTO_INCREMENT, `date_time` datetime NOT NULL, PRIMARY KEY(`dateid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `date` (`date_time`) VALUES ( '2017-08-01 15:30:02');
<?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); //} ?>
<!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.