Date and Time in PHP

You want to get the date and time from PHP.

Use date() to format a local time, date, or both. It returns a formatted string based on formatting options.

date (format [, timestamp])

Option Description
a am or pm
A AM or PM
B Swatch Internet time, a universal time scheme. More information is available at http://www.swatch.com.
c ISO 8601 date. A date is represented as YYYY-MM-DD. An uppercase T separates the date from the time. The time is represented as HH:MM:SS. The time zone is represented as an offset from Greenwich mean time (GMT) (only in PHP 5).
d Day of the month, two digits with leading zeros
D day of the week in text, three letters
F Month in text
g Hour, 12-hour format without leading zeros
G Hour, 24-hour format without leading zeros
h Hour, 12-hour format with leading zeros
H Hour, 24-hour format
i Minutes with leading zeros
I 1 if Daylight Savings Time, 0 if not
j Day of the month without leading zeros
l Day of the week in text
L 1, if it is a leap year, 0 if not
m Month with leading zeros
M Month in text, three letters
n Months without leading zeros
r RFC 822 formatted date
s Seconds with leading zeros
S English ordinal suffix, textual, two characters
t Number of days in the given month
T Time zone setting of this machine
U Seconds since the epoch
w Day of the week, numeric (0 [Sunday])
Y year, four digits
y year, two digits
z Day of the year
Z Time zone offset in seconds

<!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 in PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

echo '<p>date("d-M-Y") =>', date("d-M-Y"), "</p>";
echo '<p>date("M/d/Y") =>', date("M/d/Y"), "</p>";
echo '<p>date("D ds M, Y h:i a") =>', date("D ds M, Y h:i a"), "</p>";
echo '<p>date("\T\h\e \\t\i\m\e \i\s: h:m a") =>', date("\T\h\e \\t\i\m\e \i\s: h:m a"), "</p>";
echo '<p>date("\T\o\d\a\y \i\s: l") =>', date("\T\o\d\a\y \i\s: l"), "</p>";

?>
</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