PHP Date/Time Functions

About This Index
This page is the PHP Date/Time Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn how timestamps, DateTimeImmutable, timezones, and intervals fit together on /php/date. Use this page when you need a specific function such as date(), strtotime(), or date_diff().
Start with time(), date(), and strtotime()—then learn date_create_immutable(), date_diff(), and date_default_timezone_set().
strftime(), gmstrftime(), strptime(), date_sunrise(), and date_sunset() raise a deprecation notice. They are marked Deprecated in the table below. Use date(), IntlDateFormatter, and date_sun_info() instead.
How to Call a PHP Date/Time Function
Most helpers are global functions—call them directly. The date_* and timezone_* names are procedural versions of methods on DateTime, DateTimeImmutable, and DateTimeZone:
date_default_timezone_set('UTC');
echo date('Y-m-d', strtotime('2024-01-15')); // function style
$a = date_create('2024-01-15'); // procedural
$b = date_create('2024-03-20');
echo date_diff($a, $b)->days; // 65
$c = new DateTime('2024-01-15'); // same thing, object style
$d = new DateTime('2024-03-20');
echo $c->diff($d)->days; // 65
echo timezone_name_get(timezone_open('Asia/Tokyo')); // Asia/Tokyo PHP Date/Time Functions Table
All function guides in this PHP tutorial series (51 tutorials in 6 categories).
Format & timestamps
12 functionsRead the clock, build or parse Unix timestamps, and format them as text. The gm* versions use UTC.
| Function | Description | Tutorial |
|---|---|---|
checkdate() | Validate a Gregorian date from month, day, and year. | Try it |
date() | Format a Unix timestamp in the default timezone. | Try it |
gmdate() | Format a Unix timestamp in UTC. | Try it |
idate() | Format one part of a timestamp as an integer. | Try it |
getdate() | Get date and time parts of a timestamp as an array. | Try it |
localtime() | Get the local time parts as a numeric or associative array. | Try it |
time() | Get the current Unix timestamp. | Try it |
microtime() | Get the current Unix timestamp with microseconds. | Try it |
gettimeofday() | Get the current time as an array or float. | Try it |
mktime() | Build a Unix timestamp from local date and time parts. | Try it |
gmmktime() | Build a Unix timestamp from UTC date and time parts. | Try it |
strtotime() | Parse an English date or time text into a Unix timestamp. | Try it |
Create & format objects
8 functionsCreate DateTime or DateTimeImmutable objects from text or a strict format, print them, and parse text into arrays.
| Function | Description | Tutorial |
|---|---|---|
date_create() | Create a DateTime object (alias of new DateTime). | Try it |
date_create_immutable() | Create a DateTimeImmutable object. | Try it |
date_create_from_format() | Create a DateTime from text in a given format. | Try it |
date_create_immutable_from_format() | Create a DateTimeImmutable from text in a given format. | Try it |
date_format() | Format a DateTime object as text. | Try it |
date_parse() | Parse date text into an array of parts, warnings, and errors. | Try it |
date_parse_from_format() | Parse date text in a given format into an array. | Try it |
date_get_last_errors() | Get warnings and errors from the last date parse. | Try it |
Modify & compare
12 functionsAdd, subtract, set parts, read the timestamp or timezone, and compare two dates. date_add(), date_sub(), and date_modify() change a DateTime in place.
| Function | Description | Tutorial |
|---|---|---|
date_add() | Add a DateInterval to a DateTime. | Try it |
date_sub() | Subtract a DateInterval from a DateTime. | Try it |
date_modify() | Change a DateTime with relative text such as +1 day. | Try it |
date_diff() | Get the difference between two dates as a DateInterval. | Try it |
date_date_set() | Set the year, month, and day of a DateTime. | Try it |
date_time_set() | Set the hour, minute, second, and microsecond. | Try it |
date_isodate_set() | Set a date from an ISO year, week, and day of week. | Try it |
date_timestamp_get() | Get the Unix timestamp of a DateTime. | Try it |
date_timestamp_set() | Set the date and time from a Unix timestamp. | Try it |
date_timezone_get() | Get the DateTimeZone object of a DateTime. | Try it |
date_timezone_set() | Set the timezone of a DateTime. | Try it |
date_offset_get() | Get the UTC offset in seconds for a DateTime. | Try it |
Intervals
2 functionsDescribe a length of time and print it. The result of date_diff() is also a DateInterval.
| Function | Description | Tutorial |
|---|---|---|
date_interval_create_from_date_string() | Create a DateInterval from relative text such as 10 days. | Try it |
date_interval_format() | Format a DateInterval with % codes such as %a days. | Try it |
Timezone helpers
11 functionsSet the default timezone, open a DateTimeZone, and look up names, offsets, transitions, and abbreviations. Prefer names such as Asia/Kolkata over abbreviations.
| Function | Description | Tutorial |
|---|---|---|
date_default_timezone_get() | Get the default timezone used by all date functions. | Try it |
date_default_timezone_set() | Set the default timezone used by all date functions. | Try it |
timezone_open() | Create a DateTimeZone object (alias of new DateTimeZone). | Try it |
timezone_name_get() | Get the name of a DateTimeZone. | Try it |
timezone_offset_get() | Get the UTC offset of a timezone for a given date. | Try it |
timezone_location_get() | Get the country code, latitude, and longitude of a timezone. | Try it |
timezone_transitions_get() | Get daylight saving transitions of a timezone. | Try it |
timezone_identifiers_list() | List all timezone identifiers, optionally by group or country. | Try it |
timezone_abbreviations_list() | List timezone abbreviations and their offsets. | Try it |
timezone_name_from_abbr() | Find a timezone name from an abbreviation or offset. | Try it |
timezone_version_get() | Get the version of the timezone database in use. | Try it |
Sun & legacy
6 functionsdate_sun_info() is current. strftime(), gmstrftime(), strptime(), date_sunrise(), and date_sunset() are deprecated as of PHP 8.1.
| Function | Description | Tutorial |
|---|---|---|
date_sun_info() | Get sunrise, sunset, and twilight times for a place and day. | Try it |
date_sunrise()Deprecated | Get the sunrise time for a place and day. | Try it |
date_sunset()Deprecated | Get the sunset time for a place and day. | Try it |
strftime()Deprecated | Format local time with the system locale. | Try it |
gmstrftime()Deprecated | Format UTC time with the system locale. | Try it |
strptime()Deprecated | Parse a date string made by strftime() into an array. | Try it |
Frequently Asked Questions
Start with checkdate()
Open the first guide—syntax, examples, and FAQs.
6 people found this page helpful
