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.

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.
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.
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.
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
createFromDateString() Sets up a DateInterval from the relative parts of the string.

PHP DatePeriod Class

ClassDescription
DatePeriod The DatePeriod class.

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_offset_get() Returns the timezone offset. Alias of DateTime::getOffset().
date_interval_create_from_date_string() Sets up a DateInterval from the relative parts of the string. Alias of DateInterval::createFromDateString().
date_isodate_set() Sets the ISO date. Alias of DateTime::setISODate().
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.
idate() Format a local time/date as integer.
mktime() Gets Unix timestamp for a date.
date() Formats a local time/date.
strtotime() Parse about any English textual datetime description into a Unix timestamp.
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_open() Creates new DateTimeZone object. Alias of DateTimeZone::__construct().
timezone_version_get() Gets the version of the timezonedb.

Supported Date and Time Formats

PHP Filter 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)

5