PHP Date/Time Functions

Beginner
5 min read
Updated: Nov 2026
51 function guides
Function index

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().

Beginner path.

Start with time(), date(), and strtotime()—then learn date_create_immutable(), date_diff(), and date_default_timezone_set().

Deprecated in PHP 8.1.

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:

PHP
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 functions

Read the clock, build or parse Unix timestamps, and format them as text. The gm* versions use UTC.

FunctionDescriptionTutorial
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 functions

Create DateTime or DateTimeImmutable objects from text or a strict format, print them, and parse text into arrays.

FunctionDescriptionTutorial
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 functions

Add, subtract, set parts, read the timestamp or timezone, and compare two dates. date_add(), date_sub(), and date_modify() change a DateTime in place.

FunctionDescriptionTutorial
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 functions

Describe a length of time and print it. The result of date_diff() is also a DateInterval.

FunctionDescriptionTutorial
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 functions

Set the default timezone, open a DateTimeZone, and look up names, offsets, transitions, and abbreviations. Prefer names such as Asia/Kolkata over abbreviations.

FunctionDescriptionTutorial
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 functions

date_sun_info() is current. strftime(), gmstrftime(), strptime(), date_sunrise(), and date_sunset() are deprecated as of PHP 8.1.

FunctionDescriptionTutorial
date_sun_info()Get sunrise, sunset, and twilight times for a place and day.Try it
date_sunrise()DeprecatedGet the sunrise time for a place and day.Try it
date_sunset()DeprecatedGet the sunset time for a place and day.Try it
strftime()DeprecatedFormat local time with the system locale.Try it
gmstrftime()DeprecatedFormat UTC time with the system locale.Try it
strptime()DeprecatedParse a date string made by strftime() into an array.Try it

Frequently Asked Questions

They are built-in functions for reading the current time, turning text into timestamps, formatting dates, creating and changing DateTime objects, working with intervals, converting between timezones, and calculating sun times. Most object methods also have a procedural date_* or timezone_* name.
The Date/Time page teaches the ideas: timestamps, DateTimeImmutable versus procedural calls, timezones, formatting, and intervals. This index lists every function tutorial so you can open date(), strtotime(), date_diff(), and the rest.
Start with time(), date(), and strtotime() to understand Unix timestamps and format characters. Then learn date_create_immutable(), date_diff(), and date_default_timezone_set() for real applications.
date() formats a timestamp in the default timezone, while gmdate() always formats it in UTC. The same rule applies to mktime() and gmmktime(), and to strftime() and gmstrftime().
strftime(), gmstrftime(), strptime(), date_sunrise(), and date_sunset() are deprecated as of PHP 8.1. Use date() or DateTime::format() for formatting, IntlDateFormatter for localized output, date_parse_from_format() for strict parsing, and date_sun_info() for sun times.
Not usually. Each one matches a method on DateTime or DateTimeImmutable, such as date_diff($a, $b) and $a->diff($b). The procedural names are useful in older code and when you prefer a function-style API. Note that date_add(), date_sub(), and date_modify() only accept a DateTime object, not DateTimeImmutable.

Start with checkdate()

Open the first guide—syntax, examples, and FAQs.

checkdate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful