Latest web development tutorials

PHP gmdate () function

PHP Date / Time PHP Date / Time Reference Manual

Examples

Formatting GMT / UTC date and time, and returns the formatted date string:

<?php
// Prints the day
echo gmdate("l") . "<br>";

// Prints the day, date, month, year, time, AM or PM
echo gmdate("l jS of F Y h:i:s A");
?>

Running instance »

Definition and Usage

gmdate () function formats GMT / UTC date and time, and returns the formatted date string.

grammar

gmdate( format,timestamp);

参数 描述
format 必需。规定输出日期字符串的格式。可使用下列字符:
  • d - 一个月中的第几天(从 01 到 31)
  • D - 星期几的文本表示(用三个字母表示)
  • j - 一个月中的第几天,不带前导零(1 到 31)
  • l('L' 的小写形式)- 星期几的完整的文本表示
  • N - 星期几的 ISO-8601 数字格式表示(1 表示 Monday[星期一],7 表示 Sunday[星期日])
  • S - 一个月中的第几天的英语序数后缀(2 个字符:st、nd、rd 或 th。与 j 搭配使用)
  • w - 星期几的数字表示(0 表示 Sunday[星期日],6 表示 Saturday[星期六])
  • z - 一年中的第几天(从 0 到 365)
  • W - 用 ISO-8601 数字格式表示一年中的星期数字(每周从 Monday[星期一]开始)
  • F - 月份的完整的文本表示(January[一月份] 到 December[十二月份])
  • m - 月份的数字表示(从 01 到 12)
  • M - 月份的短文本表示(用三个字母表示)
  • n - 月份的数字表示,不带前导零(1 到 12)
  • t - 给定月份中包含的天数
  • L - 是否是闰年(如果是闰年则为 1,否则为 0)
  • o - ISO-8601 标准下的年份数字
  • Y - 年份的四位数表示
  • y - 年份的两位数表示
  • a - 小写形式表示:am 或 pm
  • A - 大写形式表示:AM 或 PM
  • B - Swatch Internet Time(000 到 999)
  • g - 12 小时制,不带前导零(1 到 12)
  • G - 24 小时制,不带前导零(0 到 23)
  • h - 12 小时制,带前导零(01 到 12)
  • H - 24 小时制,带前导零(00 到 23)
  • i - 分,带前导零(00 到 59)
  • s - 秒,带前导零(00 到 59)
  • u - 微秒(PHP 5.2.2 中新增的)
  • e - 时区标识符(例如:UTC、GMT、Atlantic/Azores)
  • I(i 的大写形式)- 日期是否是在夏令时(如果是夏令时则为 1,否则为 0)
  • O - 格林威治时间(GMT)的差值,单位是小时(实例:+0100)
  • P - 格林威治时间(GMT)的差值,单位是 hours:minutes(PHP 5.1.3 中新增的)
  • T - 时区的简写(实例:EST、MDT)
  • Z - 以秒为单位的时区偏移量。UTC 以西时区的偏移量为负数(-43200 到 50400)
  • c - ISO-8601 标准的日期(例如 2013-05-05T16:34:42+00:00)
  • r - RFC 2822 格式的日期(例如 Fri, 12 Apr 2013 12:01:05 +0200)
  • U - 自 Unix 纪元(January 1 1970 00:00:00 GMT)以来经过的秒数

同时,也可使用下列预定义常量(从 PHP 5.1.0 开始可用):

  • 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 - 与 DATE_ATOM 相同(从 PHP 5.1.3 开始)
  • DATE_RSS - RSS(Fri, 12 Aug 2013 15:52:01 +0000)
  • DATE_W3C - 万维网联盟(例如:2013-04-12T15:52:01+00:00)
timestamp 可选。规定一个整数的 Unix 时间戳。默认是当前的本地时间(time())。

technical details

return value: If successful formatted date string is returned, if it fails E_WARNING error message and return FALSE.
PHP version: 4+
Update log: PHP 5.1.0: the effective range of the timestamp is from December 13, 1901 20:45:54 GMT Friday, January 19, 2038 03:14:07 GMT Tuesday. 5.1.0 previous version on some systems (such as Windows) is limited to the time stamp from 01-01-1970 to 19-01-2038.
PHP 5.1.1: Added standard date / time format constant that specifies the format parameter.


PHP Date / Time PHP Date / Time Reference Manual