date function converts timestamps into different types of strings.
PHP date() Function has the following format.
date(format,timestamp);
Parameter | Is Required | Description |
---|---|---|
format | Required. | Format of the outputted date string. |
timestamp | Optional. | Integer Unix timestamp. Default is the current local time (time()) |
The complete list of date format characters used in date() is shown in the following table. They are case-sensitive!
Format character | Description | Example |
---|---|---|
a | Lowercase am/pm | am or pm |
A | Uppercase am/pm | AM or PM |
B | Swatch Internet Time | 000 to 999 |
c | ISO 8601 date, time, and time zone | 2004-06-18T09:26:55+01:00 |
d | 2-digit day of month, leading zeros | 01 to 31 |
D | Day string, three letters | Mon, Thu, Sat |
F | Month string, full | January, August |
g | 12-hour clock hour, no leading zeros | 1 to 12 |
G | 24-hour clock hour, no leading zeros | 0 to 23 |
h | 12-hour clock hour, leading zeros | 01 to 12 |
H | 24-hour clock hour, leading zeros | 00 to 23 |
i | Minutes with leading zeros | 00 to 59 |
I | Is daylight savings time active? | 1 if yes, 0 if no |
j | Day of month, no leading zeros | 1 to 31 |
l | Day string, full | Monday, Saturday |
L | Is it a leap year? | 1 if yes, 0 if no |
m | Numeric month, leading zeros | 01 to 12 |
M | Short month string | Jan, Aug |
n | Numeric month, no leading zeros | 1 to 12 |
O | Difference from GMT | 200 |
r | RFC-822 formatted date | Sat, 22 Dec 1979 17:30 +0000 |
s | Seconds, with leading zeros | 00 to 59 |
S | English ordinal suffix for day number | st, nd, rd, or th |
t | Number of days in month | 28 to 31 |
T | Time zone for server | GMT, CET, EST |
U | Unix Timestamp | 1056150334 |
w | Numeric day of week | 0 (Sunday), 6 (Saturday) |
W | ISO-8601 week number of year | 30 (30th week of the year) |
y | Two-digit representation of year | 97, 02 |
Y | Four-digit representation of year | 1997, 2002 |
z | Day of year | 0 to 366 |
Z | Time zone offset in seconds | -43200 to 43200 |
DATE_ATOM | Atom | 2013-04-12T15:52:01+00:00 |
DATE_COOKIE | HTTP Cookies | Friday, 12-Apr-13 15:52:01 UTC |
DATE_ISO8601 | ISO-8601 | 2013-04-12T15:52:01+0000 |
DATE_RFC822 | RFC 822 | Fri, 12 Apr 13 15:52:01 +0000 |
DATE_RFC850 | RFC 850 | Friday, 12-Apr-13 15:52:01 UTC |
DATE_RFC1036 | RFC 1036 | Fri, 12 Apr 13 15:52:01 +0000 |
DATE_RFC1123 | RFC 1123 | Fri, 12 Apr 2013 15:52:01 +0000 |
DATE_RFC2822 | RFC 2822 | Fri, 12 Apr 2013 15:52:01 +0000 |
DATE_RFC3339 | Same as DATE_ATOM (since PHP 5.1.3) | 2013-04-12T15:52:01+00:00 |
DATE_RSS | RSS | Fri, 12 Aug 2013 15:52:01 +0000 |
DATE_W3C | World Wide Web Consortium | 2013-04-12T15:52:01+00:00 |
PHP date() Function returns a formatted date string on success. FALSE on failure + an E_WARNING.
The following code prints out the current time in 24-hour clock format:
<?PHP//from w w w . ja v a2s . co m
print date("H:i");
print "The day yesterday was " . date("l", time() - 86400);
echo "\n";
$hour = date( "G" );
$year = date( "Y" );
echo $hour;
echo "\n";
echo $year;
// Prints the day
echo date("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo date("l jS \of F Y h:i:s A");
?>
The code above generates the following result.
This next example outputs the date in the format of 31st of August 2012.
<?PHP
print date("jS of F Y");
?>
The code above generates the following result.
The following code shows how to get hour and year from current date and time.
/*ww w. j a va 2s . c o m*/
<!DOCTYPE html>
<html>
<body>
<?php
$hour = date( "G" );
$year = date( "Y" );
if ( $hour >= 5 && $hour < 12 ) {
echo "<h1>Good morning!</h1>";
} elseif ( $hour >= 12 && $hour < 18 ) {
echo "<h1>Good afternoon!</h1>";
} elseif ( $hour >= 18 && $hour < 22 ) {
echo "<h1>Good evening!</h1>";
} else {
echo "<h1>Good night!</h1>";
}
$leapYear = false;
if ( ( ( $year % 4 == 0 ) && ( $year % 100 != 0 ) ) || ( $year % 400 == 0 ) )
$leapYear = true;
echo "<p>Did you know that $year is" . ( $leapYear ? "" : " not" ) . " a leap year?</p>";
?>
</body>
</html>
The code above generates the following result.
The following code shows how to get number days any month.
<?php
$lastday = mktime(0, 0, 0, 2, 1, 2010);
printf("There are %d days in February, 2010.", date("t",$lastday));
?>
The code above generates the following result.
The following code shows how to get number days current month.
<?php
printf("There are %d days in %s.", date("t"), date("F"));
?>
The code above generates the following result.
The following code shows how to get today's day of the week.
<html>//from www . j a v a 2 s.c om
<body>
<h1>
<?php
$Today = date("l");
if($Today == "Friday")
{
print("Thank goodness it's Friday!");
}
else
{
print("Today is $Today.");
}
?>
</h1>
</body>
</html>
The code above generates the following result.
The following code shows how to format date as 'g:i:s a'.
//from w w w. j a v a 2 s .c o m
<!DOCTYPE html>
<html>
<body>
<h1>
<?php
$currentTime = date( "g:i:s a" );
echo "Hello, world! The current time is $currentTime";
?>
</h1>
</body>
</html>
The code above generates the following result.