PHP Function Reference

PHP - Date and Time



These date/time functions allow you to get the date and time from the server where your PHP scripts are running. You can use these functions to format the date and time in many different ways.

Note: These functions are dependent on the locale settings of the server. Make sure to take daylight saving time and leap years into consideration when working with these functions.

Installation

There is no installation needed to use these functions. These functions are part of the PHP core.

Runtime Configuration

The behavior of these functions is affected by settings in php.ini. See Runtime Configuration.

PHP DateTime Class

Class Description
DateTime The DateTime class.
MethodsDescription
__construct() Returns new DateTime object.
add() Adds an interval to a DateTime object.
createFromFormat() Parses a time string according to a specified format.
createFromImmutable() Returns new DateTime object encapsulating the given DateTimeImmutable object.
createFromInterface() Returns new DateTime object encapsulating the given DateTimeInterface object.
getLastErrors() Returns the warnings and errors.
modify() Alters the timestamp.
setDate() Sets the date.
setISODate() Sets the ISO date.
setTime() Sets the time.
setTimestamp() Sets the date and time based on an Unix timestamp.
setTimezone() Sets the time zone.
sub() Subtracts an interval from a DateTime object.

PHP DateTimeImmutable Class

Class Description
DateTimeImmutable The DateTimeImmutable class.
MethodsDescription
__construct() Returns new DateTimeImmutable object.
add() Adds an interval to a DateTimeImmutable object.
createFromFormat() Parses a time string according to a specified format.
createFromInterface() Returns new DateTimeImmutable object encapsulating the given DateTimeInterface object.
createFromMutable() Returns new DateTimeImmutable object encapsulating the given DateTime object.
getLastErrors() Returns the warnings and errors.
modify() Creates a new object with modified timestamp.
setDate() Sets the date.
setISODate() Sets the ISO date.
setTime() Sets the time.
setTimestamp() Sets the date and time based on an Unix timestamp.
setTimezone() Sets the time zone.
sub() Subtracts an interval to a DateTimeImmutable object.

PHP DateTimeInterface interface

Interface Description
DateTimeInterface The DateTimeInterface interface.
MethodsDescription
diff() Returns the difference between two DateTime objects.
format() Returns date formatted according to given format.
getOffset() Returns the timezone offset.
getTimestamp() Gets the Unix timestamp.
getTimezone() Return time zone relative to given DateTime.

PHP DateTimeZone Class

Class Description
DateTimeZone The DateTimeZone class.
MethodsDescription
__construct() Creates new DateTimeZone object.
getLocation() Returns location information for a timezone.
getName() Returns the name of the timezone.
getOffset() Returns the timezone offset from GMT.
getTransitions() Returns all transitions for the timezone.
listAbbreviations() Returns associative array containing dst, offset and the timezone name.
listIdentifiers() Returns a numerically indexed array containing all defined timezone identifiers.

PHP DateInterval Class

Class Description
DateInterval The DateInterval class.
MethodsDescription
__construct() Creates a new DateInterval object.
createFromDateString() Sets up a DateInterval from the relative parts of the string.
format() Formats the interval.

PHP DatePeriod Class

ClassDescription
DatePeriod The DatePeriod class.
MethodsDescription
__construct() Creates a new DatePeriod object.
getDateInterval() Gets the interval.
getEndDate() Gets the end date.
getRecurrences() Gets the number of recurrences.
getStartDate() Gets the start date.

PHP Date/Time Functions

FunctionsDescription
checkdate() Validates a Gregorian date.
date_add() Adds an interval to a DateTime object. Alias of DateTime::add().
date_create() Returns new DateTime object. Alias of DateTime::__construct().
date_create_from_format() Parses a time string according to a specified format. Alias of DateTime::createFromFormat().
date_create_immutable_from_format() Parses a time string according to a specified format. Alias of DateTimeImmutable::createFromFormat().
date_create_immutable() Returns new DateTimeImmutable object. Alias of DateTimeImmutable::__construct()
date_date_set() Sets the date. Alias of DateTime::setDate().
date_default_timezone_get() Gets the default timezone used by all date/time functions in a script.
date_default_timezone_set() Sets the default timezone used by all date/time functions in a script.
date_diff() Returns the difference between two DateTime objects. Alias of DateTime::diff().
date_format() Returns date formatted according to given format. Alias of DateTime::format().
date_get_last_errors() Returns the warnings and errors. Alias of DateTime::getLastErrors().
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string. Alias of DateInterval::createFromDateString().
date_interval_format() Formats the interval. Alias of DateInterval::format().
date_offset_get() Returns the timezone offset. Alias of DateTime::getOffset().
date_parse() Returns associative array with detailed info about given date/time.
date_parse_from_format() Get info about given date formatted according to the specified format.
date_isodate_set() Sets the ISO date. Alias of DateTime::setISODate().
date_sun_info() Returns an array with information about sunset/sunrise and twilight begin/end.
date_sunrise() Returns time of sunrise for a given day and location.
date_sunset() Returns time of sunset for a given day and location.
date_time_set() Sets the time. Alias of DateTime::setTime().
date_timestamp_get() Gets the Unix timestamp. Alias of DateTime::getTimestamp().
date_timestamp_set() Sets the date and time based on an Unix timestamp. Alias of DateTime::setTimestamp().
date_timezone_get() Return time zone relative to given DateTime. Alias of DateTime::getTimezone().
date_timezone_set() Sets the time zone. Alias of DateTime::setTimezone().
date_modify() Alters the timestamp. Alias of DateTime::modify().
date_sub() Subtracts an interval from a DateTime object. Alias of DateTime::sub().
getdate() Get date/time information.
gettimeofday() Returns current time.
gmdate() Format a GMT/UTC date/time.
gmmktime() Get Unix timestamp for a GMT date.
gmstrftime() Format a GMT/UTC time/date according to locale settings.
idate() Format a local time/date as integer.
mktime() Gets Unix timestamp for a date.
date() Formats a local time/date.
localtime() Gets the local time.
microtime() Return current Unix timestamp with microseconds.
strftime() Format a local time/date according to locale settings.
strtotime() Parse about any English textual datetime description into a Unix timestamp.
strptime() Parse a time/date generated with strftime.
time() Returns current Unix timestamp.
timezone_abbreviations_list() Returns associative array containing dst, offset and the timezone name. Alias of DateTimeZone::listAbbreviations().
timezone_identifiers_list() Returns a numerically indexed array containing all defined timezone identifiers. Alias of DateTimeZone::listIdentifiers().
timezone_name_from_abbr() Returns the timezone name from abbreviation.
timezone_location_get() Returns location information for a timezone. Alias of DateTimeZone::getLocation().
timezone_name_get() Returns the name of the timezone. Alias of DateTimeZone::getName().
timezone_offset_get() Returns the timezone offset from GMT. Alias of DateTimeZone::getOffset().
timezone_open() Creates new DateTimeZone object. Alias of DateTimeZone::__construct().
timezone_transitions_get() Returns all transitions for the timezone. Alias of DateTimeZone::getTransitions().
timezone_version_get() Gets the version of the timezonedb.

Supported Date and Time Formats

PHP Date/Time Predefined Constants

The DATE_ constants are defined and they offer standard date representations, which can be used along with the date format functions (like date() function).

Following constants specify a format returned by functions date_sunrise() and date_sunset().

ConstantsTypeDescription
SUNFUNCS_RET_TIMESTAMPIntegerTimestamp
SUNFUNCS_RET_STRINGIntegerHours:minutes (example: 08:02)
SUNFUNCS_RET_DOUBLEIntegerHours as floating point number (example 8.75)