PHP Date / Time Functions

PHP Date/Time Introduction

Date/Time functions allow you to obtain date and time from the server on which the PHP script is running. You can use Date/Time functions to format date and time in different ways.

Note:These functions depend on the local settings of the server. Remember to consider daylight saving time and leap years when using these functions.

Installation

PHP Date/Time functions are a core part of PHP. These functions can be used without installation.

Runtime Configuration

The behavior of Date/Time functions in PHP is affected by the settings in php.ini:

Name Description Default PHP Version
date.timezone Default Time Zone (used by all Date/Time functions) "" PHP Version
date.default_latitude Default Latitude (used by date_sunrise() and date_sunset()) "31.7667" PHP 5.0
date.default_longitude Default Longitude (used by date_sunrise() and date_sunset()) "35.2333" PHP 5.0
date.sunrise_zenith Default Sunrise Zenith (used by date_sunrise() and date_sunset()) "90.83" PHP 5.0
date.sunset_zenith Default Sunset Zenith (used by date_sunrise() and date_sunset()) "90.83" PHP 5.0

PHP 5 Date/Time Functions

function Description
checkdate() Validates 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 time zone used by all Date/Time functions.
date_default_timezone_set() Sets the default time zone used by all Date/Time functions.
date_diff() Returns the difference between two dates.
date_format() Returns the date formatted according to the specified format.
date_get_last_errors() Returns warnings/errors in the date string.
date_interval_create_from_date_string() Builds a DateInterval from the relevant parts of a string.
date_interval_format() Formats the time interval.
date_isodate_set() Sets ISO date.
date_modify() Modifies the timestamp.
date_offset_get() Returns the time zone offset.
date_parse_from_format() Returns an associative array with detailed information about a specified 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 the sunrise/sunset and twilight start/end times at a specified date and location.
date_sunrise() Returns the sunrise time at a specified date and location.
date_sunset() Returns the sunset time at a specified date and location.
date_time_set() Sets the time.
date_timestamp_get() Returns Unix timestamp.
date_timestamp_set() Sets the date and time based on Unix timestamp.
date_timezone_get() Returns the time zone of the given DateTime object.
date_timezone_set() Sets the timezone for the DateTime object.
date() Formats local date and time.
getdate() Returns a timestamp or the date/time information of the current local date/time.
gettimeofday() Returns the current time.
gmdate() Formats GMT/UTC date and time.
gmmktime() Returns the UNIX timestamp of the GMT date.
gmstrftime() Formats GMT/UTC date and time according to the locale.
idate() Formats local time/date as an integer.
localtime() Returns the local time.
microtime() Returns the microseconds as part of the current time.
mktime() Returns the Unix timestamp of the date.
strftime() Formats local time/date according to the locale.
strptime() Parses a time/date string generated by strftime().
strtotime() Parses any English text description of a date or time into a Unix timestamp.
time() Returns the current time as a Unix timestamp.
timezone_abbreviations_list() Returns an associative array containing daylight saving time, offset, and timezone name.
timezone_identifiers_list() Returns an indexed array with all timezone identifiers.
timezone_location_get() Returns the location information for the specified timezone.
timezone_name_from_abbr() Returns the name of the timezone based on the timezone abbreviation.
timezone_name_get() Returns the name of the timezone.
timezone_offset_get() Returns the timezone offset from 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

Constants Description
DATE_ATOM Atom (for example: 2005-08-15T16:13:03+0000)
DATE_COOKIE HTTP Cookies (for example: 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 (Sun, 14 Aug 2005 16:13:03 +0000)
DATE_RSS RSS (Sun, 14 Aug 2005 16:13:03 UTC)
DATE_W3C World Wide Web Consortium (e.g., 2005-08-14T16:13:03+0000)