PHP 5 Date/Time
Functions
PHP Date/Time Introduction
The Date/Time functions allow you to get the date and time from the server where your PHP script runs. You can use the Date/Time functions to format the date and time in several ways.
Note: These functions rely on the server's local settings. Remember to take daylight saving time and leap years into account when using these functions.
Installation
The PHP Date/Time functions are part of the PHP core. There is no installation needed to use these functions.
Runtime Configuration
The behavior of the Date/Time functions is affected by settings in the php.ini file:
Name | Description | Default | PHP Version |
---|---|---|---|
date.timezone | Specifies the default timezone (used by all Date/Time functions) | "" | PHP 5.1 |
date.default_latitude | Specifies the default latitude (used by date_sunrise() and date_sunset()) | "31.7667" | PHP 5.0 |
date.default_longitude | Specifies the default longitude (used by date_sunrise() and date_sunset()) | "35.2333" | PHP 5.0 |
date.sunrise_zenith | Specifies the default sunrise zenith (used by date_sunrise() and date_sunset()) | "90.83" | PHP 5.0 |
date.sunset_zenith | Specifies the default sunset zenith (used by date_sunrise() and date_sunset()) | "90.83" | PHP 5.0 |
PHP 5 Date/Time Functions
Function | Description |
---|---|
checkdate() | Validates a Gregorian date. |
date_add() | Adds days, months, years, hours, minutes, and seconds to a date. |
date_create_from_format() | Returns a new DateTime object formatted according to the specified format. |
date_create() | Returns a new DateTime object. |
date_date_set() | Sets a new date. |
date_default_timezone_get() | Returns the default timezone used by all Date/Time functions. |
date_default_timezone_set() | Sets the default timezone used by all Date/Time functions. |
date_diff() | Returns the difference between two dates. |
date_format() | Returns a date formatted according to the specified format. |
date_get_last_errors() | Returns warnings/errors from a date string. |
date_interval_create_from_date_string() | Sets up a DateInterval from the relative parts of the string. |
date_interval_format() | Formats the interval. |
date_isodate_set() | Sets the ISO date. |
date_modify() | Modifies the timestamp. |
date_offset_get() | Returns the timezone offset. |
date_parse_from_format() | Returns an associative array with detailed information about the given date formatted according to the specified format. |
date_parse() | Returns an associative array with detailed information about a specified date. |
date_sub() | Subtracts days, months, years, hours, minutes, and seconds from a specified date. |
date_sun_info() | Returns an array containing information about sunrise/sunset and twilight start/end for a specified date and location. |
date_sunrise() | Returns the sunrise time for a specified date and location. |
date_sunset() | Returns the sunset time for a specified date and location. |
date_time_set() | Sets the time. |
date_timestamp_get() | Returns the Unix timestamp. |
date_timestamp_set() | Sets the date and time based on a Unix timestamp. |
date_timezone_get() | Returns the timezone of the given DateTime object. |
date_timezone_set() | Sets the timezone of the DateTime object. |
date() | Formats a local date and time. |
getdate() | Returns the date/time information for a timestamp or the current local date/time. |
gettimeofday() | Returns the current time. |
gmdate() | Formats a GMT/UTC date and time. |
gmmktime() | Returns the Unix timestamp for a GMT date. |
gmstrftime() | Formats a GMT/UTC date and time according to locale settings. |
idate() | Formats a local time/date as integer. |
localtime() | Returns the local time. |
microtime() | Returns the microseconds of the current Unix timestamp. |
mktime() | Returns the Unix timestamp for a date. |
strftime() | Formats a local time/date according to locale settings. |
strptime() | Parses a time/date generated with strftime(). |
strtotime() | Parses any English textual datetime description into a Unix timestamp. |
time() | Returns the current Unix timestamp. |
timezone_abbreviations_list() | Returns an associative array containing DST, offset, and timezone names. |
timezone_identifiers_list() | Returns a numerically indexed array with all timezone identifiers. |
timezone_location_get() | Returns location information for a specified timezone. |
timezone_name_from_abbr() | Returns the timezone name from abbreviation. |
timezone_name_get() | Returns the name of the timezone. |
timezone_offset_get() | Returns the timezone offset relative to GMT. |
timezone_open() | Creates a new DateTimeZone object. |
timezone_transitions_get() | Returns all transitions for the timezone. |
timezone_version_get() | Returns the version of the timezone database. |
PHP 5 Predefined Date/Time Constants
Constant | Description |
---|---|
DATE_ATOM | Atom (e.g., 2005-08-15T16:13:03+0000) |
DATE_COOKIE | HTTP Cookies (e.g., Sun, 14 Aug 2005 16:13:03 UTC) |
DATE_ISO8601 | ISO-8601 (e.g., 2005-08-14T16:13:03+0000) |
DATE_RFC822 | RFC 822 (e.g., Sun, 14 Aug 2005 16:13:03 UTC) |
DATE_RFC850 | RFC 850 (e.g., Sunday, 14-Aug-05 16:13:03 UTC) |
DATE_RFC1036 | RFC 1036 (e.g., Sunday, 14-Aug-05 16:13:03 UTC) |
DATE_RFC1123 | RFC 1123 (e.g., Sun, 14 Aug 2005 16:13:03 UTC) |
DATE_RFC2822 | RFC 2822 (e.g., Sun, 14 Aug 2005 16:13:03 +0000) |
DATE_RSS | RSS (e.g., Sun, 14 Aug 2005 16:13:03 UTC) |
DATE_W3C | World Wide Web Consortium (e.g., 2005-08-14T16:13:03+0000) |