Format the local date and time and return the formatted date string:
<?php // Set time zone date_default_timezone_set ( " PRC " ) ; // Print the current time PHP_EOL newline character, compatible with different systems echo date ( " Ymd H:i:s " ) . PHP_EOL ; echo date ( " Y year m month d day H point i minute s second " ) . PHP_EOL ; // Specify time $time = strtotime ( " 2018-01-18 08:08:08 " ) ; // Convert the specified date into a timestamp echo date ( " Ymd H:i:s " , $time ) . PHP_EOL ; ?>The output looks like this:
2018-01-31 22:09:35 January 31, 2018 22:09:35 2018-01-18 08:08:08
The date() function formats the local date and time and returns the formatted date string.
date( format,timestamp);
Returns a string resulting from the integer timestamp according to the given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default value is time().
parameter | describe |
---|---|
format | Required. Specifies the format of the output date string. The following characters can be used: d - day of the month (from 01 to 31) D - textual representation of the day of the week (in three letters) j - Day of the month, without leading zeros (1 to 31) l (lowercase 'L') - the full textual representation of the day of the week N - Day of the week in ISO-8601 numeric format (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 - the numeric day of the week (0 means Sunday, 6 means Saturday) z - day of the year (from 0 to 365) W - Represents the day of the year in ISO-8601 numeric format (week starts on Monday) F - The complete text representation of the month (January to December) m - numeric representation of the month (from 01 to 12) M - A short text representation of the month (represented by three letters) n - the numeric representation of the month, without leading zeros (1 to 12) t - the number of days in a given month L - whether it is a leap year (1 if it is a leap year, 0 otherwise) o - year number according to ISO-8601 standard Y - Four-digit representation of the year y - two-digit year a - lowercase: am or pm A - Capital form: AM or PM B - Swatch Internet Time (000 to 999) g - 12-hour clock without leading zeros (1 to 12) G - 24-hour clock without leading zeros (0 to 23) h - 12-hour clock with leading zeros (01 to 12) H - 24-hour clock with leading zeros (00 to 23) i - cents, with leading zeros (00 to 59) s - seconds with leading zeros (00 to 59) u - microseconds (new in PHP 5.2.2) e - Time zone identifier (for example: UTC, GMT, Atlantic/Azores) I (capital form of i) - Whether the date is in daylight saving time (1 if it is daylight saving time, 0 otherwise) O - Difference from Greenwich Mean Time (GMT) in hours (example: +0100) P - The difference between Greenwich Mean Time (GMT), in hours:minutes (new in PHP 5.1.3) T - Abbreviation for time zone (examples: EST, MDT) Z - Time zone offset in seconds. Negative offsets for time zones west of UTC (-43200 to 50400) c - ISO-8601 standard date (e.g. 2013-05-05T16:34:42+00:00) r - Date in RFC 2822 format (e.g. Fri, 12 Apr 2013 12:01:05 +0200) U - Number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) Also, the following predefined constants can be used (available since PHP 5.1.0): DATE_ATOM - Atom (for example: 2013-04-12T15:52:01+00:00) DATE_COOKIE - HTTP Cookies (example: Friday, 12-Apr-13 15:52:01 UTC) DATE_ISO8601 - ISO-8601 (for example: 2013-04-12T15:52:01+0000) DATE_RFC822 - RFC 822 (Example: Fri, 12 Apr 13 15:52:01 +0000) DATE_RFC850 - RFC 850 (example: Friday, 12-Apr-13 15:52:01 UTC) DATE_RFC1036 - RFC 1036 (Example: Fri, 12 Apr 13 15:52:01 +0000) DATE_RFC1123 - RFC 1123 (Example: 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 (as of PHP 5.1.3) DATE_RSS - RSS (Fri, 12 Aug 2013 15:52:01 +0000) DATE_W3C - World Wide Web Consortium (example: 2013-04-12T15:52:01+00:00) |
timestamp | Optional. Specifies an integer Unix timestamp. The default is the current local time (time()). |
Return value: | If successful, a formatted date string is returned. If failed, an E_WARNING error is reported and FALSE is returned. |
---|---|
PHP version: | 4+ |
Update log: | PHP 5.1.0: Added E_STRICT and E_NOTICE time zone errors. 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 (e.g. Windows) timestamps were limited to from 01-01-1970 to 19-01-2038. PHP 5.1.1: New standard date/time format constants for specifying the format parameter. |