strftime()

The strftime() function is also used to format the date and time (based on the locale settings), similar to the date() function but with different options.

strftime (format [, timestamp])

Format Character Description
%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Preferred date and time representation
%C Century number
%d Day of the month as a decimal number
%D Same as %m/%d/%y
%e Day of the month as a decimal number, a single digit is preceded by a space
%g Like %G, but without the century
%G The four-digit year corresponding to the ISO week number
%h Same as %b
%H Hour as a decimal number using a 24-hour clock
%I Hour as a decimal number using a 12-hour clock
%j Day of the year as a decimal number
%m Month as a decimal number
%M Minute as a decimal number
%n new line character
%p Either am or pm according to the current locale
%r Time in am or pm notation
%R Time in 24-hour notation
%S Second as a decimal number
%t tab character
%T Current time, equal to %H:%M:%S
%u Weekday as a decimal number (a [Monday])
%U Week number of the current year as a decimal number, starting with the first Sunday as the first day of the week.
%V The ISO 8601:1988 week number of the current year as a decimal number, where 1 is the first week that has at least four days in the current year, and with Monday as the first day of the week.
%W Week number of the current year as a decimal number, starting with the first Monday as the first day of the first week.Week numb
%w day of the week as a decimal, Sunday being 0.
%x Preferred date representation for the current locale without the time.
%X Preferred time representation for the current locale without the time.
%y Year as a decimal number without a century
%Y Year as a decimal number including the century
%Z or %z Time zone or name or abbreviation
%% 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>strftime()</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>Using strftime()</h2>
	<form action = "$_SERVER[SCRIPT_NAME]" method="post">
	Month:
	<input type="text" name="mm" size="50" value="07" />
	<br />
	Day:
	<input type="text" name="dd" size="50" value="04" />
	<br />
	Year:
	<input type="text" name="yy" size="50" value="2008" />
	<br />
	<input type="submit" name="submit" value="Submit" />
	</form>
HTML;
}

function process_form() {

	$mm = "$_POST[mm]";
	$dd = "$_POST[dd]";
	$yy = "$_POST[yy]";
	$date = mktime(0,0,0, $mm, $dd, $yy);

	echo "Today is ", strftime("%A, %x"), " and the time is ", strftime("%X"), "<br />";
	echo strftime("the date is %m/%d/%Y", $date), "<br />";
	echo strftime("the month is %B", $date), "<br />";
	echo strftime("the day is %A", $date), "<br />";
	echo strftime("the year is %Y", $date), "<br />";

	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