English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Date & Time Functions

These functions allow you to obtain the date and time from the server running the PHP script. You can use these functions to format dates and times in many different ways.

Installation

These functions can be used without installation. They are part of the PHP core.

Date/Time configuration options:

NameDefaultDescriptionVariable
date.default_latitude"" 31.7667""Specify default latitude (used by date_sunrise() and date_sunset())PHP_INI_ALL
date.default_longitude"" 35.2333""Specify default longitude (used by date_sunrise() and date_sunset())PHP_INI_ALL
date.sunrise_zenith"" 90.83""Specify default sunrise zenith (used by date_sunrise() and date_sunset())PHP_INI_ALL
date.sunset_zenith"" 90.83""Specify default sunset zenith (used by date_sunrise() and date_sunset())PHP_INI_ALL
date.timezone""Specify default timezonePHP_INI_ALL

Version-Indicates the earliest PHP version that supports this function.

NumberFunction nameFunction descriptionVersion
1checkdate()Validate Gregorian date3
2date_create()Return a new DateTime object5
3date_date_set()Set a new date.5
4date_default_timezone_get()Return the default timezone, used by all Date/Time function usage.5
5date_default_timezone_set()Set default timezone, used by all Date/Time function usage.5
6date_format()Return a date formatted according to the given format5
7date_isodate_set()Set ISO date5
8date_modify()Modify timestamp5
9date_offset_get()Return the daylight saving time offset5
10date_parse()Return an associative array with detailed information for the given date5
11date_sun_info()Return an array containing information about the sunrise on the specified date and location/Dusk and dusk start/An array of information about the end of dusk.5
12date_sunrise()Return the given date/The sunrise time of the location5
13date_sunset()Return the given date/The sunset time of the location5
14date_time_set()Set time5
15date_timezone_get()Return the timezone of the given DateTime object.5
16date_timezone_set()Set the timezone of the DateTime object5
17date()Format local date and time3
18getdate()Return an array containing date and time information of the Unix timestamp3
19gettimeofday()Return an array containing information about the current time3
20gmdate()Format GMT/UTC date/Time3
21gmmktime()Return the Unix timestamp of the GMT date3
22gmstrftime()

Format GMT according to the language environment/UTC time/Date

3
23idate()Convert local time/Format date as an integer5
24localtime()Return local time.4
25microtime()Return the microseconds of the current Unix timestamp3
26mktime()Return the Unix timestamp of a date3
27strftime()Format local time according to the locale/Date3
28strptime()Parse the time generated by strftime()/Date5
29strtotime()Parse any English text description of a date or time into a Unix timestamp3
30time()Return the Unix timestamp of the current time3
31timezone_abbreviations_list()Return an associative array containing daylight saving time, offset, and timezone name5
32timezone_identifiers_list()Return an array of numeric indices with all timezone identifiers5
33timezone_name_from_abbr()Return the timezone name from abbreviation5
34timezone_name_get()Return the name of the timezone5
35timezone_offset_get()Return the timezone offset from Greenwich Mean Time5
36timezone_open()Return a new DateTimeZone object5
37timezone_transitions_get()Return all timezone conversions when returning5
38date_add()Adds days, months, years, hours, minutes, and seconds to a date5.3
39date_create_from_format()Creates a date by parsing a time string according to the specified format5.3
40date_diff()Calculates and returns the difference between two dates5.3
41date_parse_from_format()Returns information about a given date according to the specified format5.3
42date_parse()Returns an associative array with detailed information about a specified date5.2
43date_sub()Subtracts days, months, years, hours, minutes, and seconds from a specified date5.3
44date_timestamp_get()Returns Unix timestamp5.3
45date_timestamp_set()Sets date and time values according to the given timestamp5.3
46date_get_last_errors()Returns warnings and errors when creating a DateTime object5.3
47date_interval_create_from_date_string()Creates a date interval from a given string5
48date_interval_format()Formats a time interval5.5
49date_create_immutable_from_format()Parses a time string according to the specified format5.5
50date_create_immutable()Creates and returns a DateTimeImmutable object5.5
51timezone_version_get()Returns the version of the current time zone database5.3
52timezone_location_get() Returns location information related to the time zone5.3

PHP date/Time constants

NumberConstants and descriptions
1

DATE_ATOM

atomic (Example:2005-08-15T16:13:03 + 0000)

2

DATE_COOKIE

HTTP Cookies (for example: Sun,2005year8month14day16:13:03 UTC)

3

DATE_ISO8601

ISO-8601(For example:2005-08-14T16:13:03 + 0000)

4

DATE_RFC822

RFC 822)(Example: Sun,2005year8month14day16:13:03 UTC)

5

DATE_RFC850

RFC 850 (Example: UTC 05year8month14day16:13:03)

6

DATE_RFC1036

RFC 1036(Example: UTC 05year8month14day16:13:03)

7

DATE_RFC1123RFC

RFC 1123)(Example: Sun,2005year8month14day16:13:03 UTC)

8

DATE_RFC2822

RFC 2822(2005year8month14day, Sunday16:13:03 +0000)

9

DATE_RSS

RSS (Sunday,2005year8month14day16:13:03 UTC)

10

DATE_W3C

World Wide Web Consortium (for example:2005-08-14T16:13:03 + 0000)

11

SUNFUNCS_RET_TIMESTAMP

timestamp (in5.1.2available in Chinese)

12

SUNFUNCS_RET_STRING

hours:minutes (for example: 08:02)(in5.1.2available in Chinese)

13

SUNFUNCS_RET_DOUBLE

hours as a floating-point number (for example8.75)(in5.1.2available in Chinese)