Formatting the Date and Time

When retrieving dates and times from a table, you want to format the output.

Use DATE_FORMAT() and TIME_FORMAT().

Format Character Description
%a Abbreviated weekday name
%b Abbreviated month name
%c Month (1-2
%d Day of the month as a decimal number
%D Day with a suffix (30th)
%e Day of the month as a decimal number, a single digit is preceded by a space
%f Microseconds
%H Hour (00..23)
%h Hour (01..12)
%i Minutes as a decimal number
%I Hour (01-12)
%j Day of the year as a decimal number
%k Hour (0..23)
%l r (1-12)Hou
%m Month as a decimal number with a leading 0
%M Month name
%p AM/PM
%r Time in AM or PM notation
%S Second as a decimal number
%s Second as a decimal number
%T Time, 24-hour (hh:mm:ss)
%u Week number (00..53) of the current year as a decimal number, starting with the first Monday as the first day of the week.
%U Week number (00..53) of the current year as a decimal number, starting with the first Sunday as the first day of the week.
%v Week number (01..53) of the current year as a decimal number, starting with the first Monday as the first day of the week.
%V Week number (01..53) of the current year as a decimal number, starting with the first Sunday as the first day of the week.
%W Weekday (Sunday, Monday, etc.)
%w day of the week as a decimal, Sunday being 0.
%y Year as a decimal number without a century
%Y Year as a decimal number including the century
%% A literal % character

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Date and Time Functions</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

if (isset($_POST['submit'])) {
		process_form();
	}
	else {
		display_form();// display form for the first time
	}

function display_form() {
	echo <<<HTML
	<h2>Date and Time Functions</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Username:
	<input type="text" name="username" size="50" value="hann" />
	<br />
	Password:
	<input type="password" name="password" size="50" />
	<br />
	Database name:
	<input type="text" name="db" size="50" value="hann_db" />
	<br />
	Table name:
	<input type="text" name="table" size="50" value="blog_entries" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	DEFINE ('DB_USER', "$_POST[username]");
	DEFINE ('DB_PASSWORD', "$_POST[password]");
	DEFINE ('DB_HOST', "localhost");
	DEFINE ('DB_NAME', "$_POST[db]");
	DEFINE ('TABLE_NAME', "$_POST[table]");

	echo "<p>Opening the connection to the database server.</p>";
	if ($link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) {

		// we are connected
		// use MySQL query to calculate an age in days

		// select a database
		if (@mysql_select_db(DB_NAME)) {
			echo "<P>The database ", DB_NAME, " has been selected.</p>";
		}
		else {
			die ("<p>Could not select the database because: ". mysql_error(). "</p>");
		}

		// define the query
		$query = "SELECT DATE_FORMAT(date_entered, '%M %e, %Y - %l:%i %p') FROM ". TABLE_NAME;
		// run the query
		if ($res = @mysql_query($query)) {
			$day = mysql_fetch_array($res);
			// display
				echo "$day[0]<br />";
		}
		else {
			die ("<p>Could not run query because: ". mysql_error(). "</p>");
		}

		// close the connection
		mysql_close($link);
	}
	else {
		die ("<p>Could not connect to MySQL because: ". mysql_error(). "</p>");
	}
	echo "<p><a href=\"$_SERVER[SCRIPT_NAME]\">Try again?</a></p>\n";

}

?>
</p>
</body>
</html>

View the effect


Date and Time | Introduction | Date and Time in PHP | strftime() | time() | Unix Timestamps | getdate() | Validating Dates | Calculating Dates | Date and Time Functions | Formatting the Date and Time
© 2008: Hann So
email: hso@voyager.deanza.edu