Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP date() Function

PHP date() Function

PHP Date/Time PHP Date/Time Reference Manual

Example

Format a local date and time and return the formatted date string:

The output is shown below:


2018-01-31 22:09:35
2018 年 01 月 31 日 22 点 09 分 35 秒
2018-01-18 08:08:08

Definition and Usage

The date() function formats a local date and time and returns the formatted date string.

Syntax

date(

format,timestamp);

Returns a string produced by formatting the integer timestamp according to the given format string. If no timestamp is given, the current local time is used. In other words, timestamp is optional and defaults to time().

Parameters Description
format Required. Specifies the format of the output date string. The following characters can be used:
  • d - Day of the month (01 to 31)
  • D - Textual representation of the day of the week (three letters)
  • j - Day of the month without leading zeros (1 to 31)
  • l (lowercase 'L') - Full textual representation of the day of the week
  • N - ISO-8601 numeric representation of the day of the week (1 for Monday, 7 for Sunday)
  • S - English ordinal suffix for the day of the month (2 characters: st, nd, rd, or th; used with j)
  • w - Numeric representation of the day of the week (0 for Sunday, 6 for Saturday)
  • z - Day of the year (0 to 365)
  • W - ISO-8601 numeric representation of the week number of the year (each week starts on Monday)
  • F - Full textual representation of the month (January to December)
  • m - Numeric representation of the month (01 to 12)
  • M - Short textual representation of the month (three letters)
  • n - Numeric representation of the month without leading zeros (1 to 12)
  • t - Number of days in the given month
  • L - Whether it is a leap year (1 if it is, 0 otherwise)
  • o - Year number according to ISO-8601
  • Y - Four-digit representation of the year
  • y - Two-digit representation of the year
  • a - Lowercase: am or pm
  • A - Uppercase: AM or PM
  • B - Swatch Internet Time (000 to 999)
  • g - 12-hour format without leading zeros (1 to 12)
  • G - 24-hour format without leading zeros (0 to 23)
  • h - 12-hour format with leading zeros (01 to 12)
  • H - 24-hour format with leading zeros (00 to 23)
  • i - Minutes with leading zeros (00 to 59)
  • s - Seconds with leading zeros (00 to 59)
  • u - Microseconds (added in PHP 5.2.2)
  • e - Time zone identifier (for example: UTC, GMT, Atlantic/Azores)
  • I (uppercase i) - Whether the date is in daylight saving time (1 if it is, 0 otherwise)
  • O - Difference to Greenwich Mean Time (GMT) in hours (example: +0100)
  • P - Difference to Greenwich Mean Time (GMT) in hours:minutes (added in PHP 5.1.3)
  • T - Time zone abbreviation (examples: EST, MDT)
  • Z - Time zone offset in seconds. Offsets for time zones west of UTC are negative (-43200 to 50400)
  • c - ISO-8601 date (for example 2013-05-05T16:34:42+00:00)
  • r - RFC 2822 formatted date (for example Fri, 12 Apr 2013 12:01:05 +0200)
  • U - Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)
  • The following predefined constants can also be used (available since PHP 5.1.0):

    | | *timestamp* | Optional. Specifies an integer Unix timestamp. Defaults to the current local time (time()). |

    Technical Details

    Return Value: Returns the formatted date string on success; on failure, raises an E_WARNING and returns FALSE.
    PHP Version: 4+
    Changelog: PHP 5.1.0: E_STRICT and E_NOTICE time zone errors were added. The valid range of timestamps is from Friday, December 13, 1901 20:45:54 GMT to Tuesday, January 19, 2038 03:14:07 GMT. Prior to 5.1.0, on some systems (such as Windows) timestamps were limited to 01-01-1970 through 19-01-2038.
    PHP 5.1.1: Standard date/time format constants were added for specifying the format parameter.

    PHP Date/Time PHP Date/Time Reference Manual