PHP gmdate – Format a UNIX time as GMT text

The gmdate function is used to convert a UNIX style time integer to a meaningful text string, according to a specially encoded format string.

Syntax:

string gmdate (string $Format)
string gmdate (string $Format, int $Time)

Arguments:
$Format is a format string; the following key characters are substituted as described:

Key Description PHP
Date/Time r RFC 2822: D, d M Y H:i:s O  
Date Year Y Year as 4 digit number  
y Year mod 100 as 2 digit number  
L 1 if a leap year, 0 if not  
Month F Month of the year as full name  
M Month of the year as three letter abbreviation  
m Month of the year as one or two digit number  
n Month of the year as two digit number  
Day
of
Year
z Day of the year – 1 as number  
Day
of
Month
d Day of the month as two digit number  
j Day of the month as one or two digit number  
jS Day of the month as one or two digit ordinal number  
Day
of
Week
D Day of the week as three letter abbreviation  
l Day of the week as full name  
w Day of the week – 1 as number  
Time Hour g Hour of the half day as one or two digit number  
h Hour of the half day as two digit number  
G Hour of the day as one or two digit number  
H Hour of the day as two digit number  
Minute i Minute of the hour as two digit number  
Second s Second of the minute as two digit number  
Millisecond u Millisecond of the second  
AM/PM a am or pm  
A AM or PM  
Beats B Swatch time  
Information t Number of days in the month  
TimeZone T Timezone name  
e Timezone abbreviatiom 5.1.0
O Offset from GMT in hundred hours  
P Offset from GMT in hours:minutes 5.1.3
Z Offset from GMT in seconds  
I 1 if Daylight Savings Time, 0 if not  
UNIX time U Seconds since midnight before Jan 1, 1970 5.2.2
ISO
8601
Date/Time c ISO-8601 Date and time 5
Year o ISO-8601 year as 4 digit number 5.1.0
Day N ISO-8601: (((Day of the week) – 2) modulo 7) + 1 5.1.0

$Time
UNIX style seconds since midnight before Jaunary 1, 1970. The default is the current system time.

The return value is a string equal to the format string with substitutions from $Time (or current system time) according to the table above.


Example code:

<?php
 
echo "When this page was loaded,\n";
echo 'It was then ', gmdate ('r'), "\n";
echo 'The currend gmdate was ', gmdate ('F j, Y'), "\n";
echo 'The currend gmdate was ', gmdate ('M j, Y'), "\n";
echo 'The currend gmdate was ', gmdate ('m/d/y'), "\n";
echo 'The currend gmdate was the ', gmdate ('jS \o\f M, Y'), "\n";
echo 'The currend time was ', gmdate ('g:i:s A T'), "\n";
echo 'The currend time was ', gmdate ('H:i:s O'), "\n";
echo gmdate ('Y');
gmdate ('L')?(print ' is'):(print ' is not');
echo " a leap year\n";
echo time ('U'), " seconds had elapsed since January 1, 1970.\n";
 
?>

Output

When this page was loaded,
It was then Sun, 27 Dec 2009 13:08:53 +0000
The currend gmdate was December 27, 2009
The currend gmdate was Dec 27, 2009
The currend gmdate was 12/27/09
The currend gmdate was the 27th of Dec, 2009
The currend time was 1:08:53 PM GMT
The currend time was 13:08:53 +0000
2009 is not a leap year
1261919333 seconds had elapsed since January 1, 1970.

Share Article/Example:
  • Facebook
  • Twitter
  • del.icio.us
  • Digg
  • DotNetKicks
  • DZone