PHP Date and Time utilities are used to format and configure dates and hours. The date() function
formats a timestamp into a more readable date and time string. Below, we will look at an example showcasing a live
clock with the current date, which is written using JavaScript, but also utilizes PHP to set the initial starting
time. The required format parameter of the date() function determines how to style the output. You can
also include separators such as forward slashes (/), dots (.), or hyphens (-) between the date characters.
Character parameters define which components of the date and time we want to output and in what specific format:
Date and time functions allow us to retrieve data directly from the server. These configurations depend on the system's regional settings. When working with these functions, attention must be paid to daylight saving time (DST) and leap years.
| Function Name | Description | Example (Code for Simulator) |
|---|---|---|
checkdate() |
Validates a Gregorian date based on the provided month, day, and year (returns true/false). | var_dump(checkdate(2, 29, 2026)); // 2026 is not a leap year |
date() |
Formats a local time and date according to the specified format characters. | echo date("d.m.Y H:i:s"); |
date_add() |
Adds an amount of days, months, years, hours, minutes, or seconds to a DateTime object. | $d = date_create("2026-06-05"); date_add($d, date_interval_create_from_date_string("10 days")); echo date_format($d, "Y-m-d"); |
date_create() |
Creates and returns a new DateTime object (defaults to current time if left empty). | $d = date_create("2026-12-25"); echo date_format($d, "d. M Y"); |
date_create_from_format() |
Parses a time string according to a specific custom format and returns a new DateTime object. | $d = date_create_from_format("j-M-Y", "5-Jun-2026"); echo date_format($d, "Y-m-d"); |
date_date_set() |
Sets a new date (year, month, day) on an existing DateTime object. | $d = date_create(); date_date_set($d, 2026, 12, 31); echo date_format($d, "Y-m-d"); |
date_default_timezone_get() |
Gets the default timezone currently used by all date/time functions in the script. | echo date_default_timezone_get(); |
date_default_timezone_set() |
Sets the default timezone used by all date/time functions in the script (e.g., Europe/Ljubljana). | date_default_timezone_set("Europe/Ljubljana"); echo date("H:i"); |
date_diff() |
Calculates and returns the difference (interval) between two DateTime objects. | $d1 = date_create("2026-06-01"); $d2 = date_create("2026-06-05"); $diff = date_diff($d1, $d2); echo $diff->format("%r%a days"); |
date_format() |
Returns a formatted date string from a DateTime object according to a specified format. | $d = date_create(); echo date_format($d, "d. m. Y"); |
date_get_last_errors() |
Returns an array of warnings and errors found while parsing a date/time string. | date_create("invalid-date-string"); print_r(date_get_last_errors()); |
date_interval_create_from_date_string() |
Sets up a DateInterval object from relative parts of a readable English string. | $int = date_interval_create_from_date_string("2 months + 5 days"); echo $int->format("%m months, %d days"); |
date_interval_format() |
Formats and outputs information stored inside a given time interval object. | $int = date_interval_create_from_date_string("1 year"); echo date_interval_format($int, "%Y year"); |
date_isodate_set() |
Sets a date on an existing object using ISO standard specifications for weeks and day-offsets. | $d = date_create(); date_isodate_set($d, 2026, 23, 1); echo date_format($d, "Y-m-d"); // Day 1 of week 23 in 2026 |
date_modify() |
Alters the timestamp of a DateTime object by incrementing or decrementing it with a relative string. | $d = date_create("2026-06-05"); date_modify($d, "+1 month"); echo date_format($d, "Y-m-d"); |
date_offset_get() |
Returns the timezone offset relative to UTC in seconds for the selected date. | $d = date_create(); echo date_offset_get($d) / 3600 . " hours"; |
date_parse() |
Parses any given date string into a detailed associative array containing all time components. | print_r(date_parse("2026-06-05 15:30:00")); |
date_parse_from_format() |
Parses a date string into an array according to a strictly defined input format. | print_r(date_parse_from_format("Y.m.d", "2026.06.05")); |
date_sub() |
Subtracts an amount of days, months, years, hours, or minutes from a DateTime object. | $d = date_create("2026-06-05"); date_sub($d, date_interval_create_from_date_string("5 days")); echo date_format($d, "Y-m-d"); |
date_sun_info() |
Returns an array containing detailed information about sunrise, sunset, and twilight for a specific location and time. | print_r(date_sun_info(time(), 46.0569, 14.5058)); // Coordinates for Ljubljana |
date_time_set() |
Sets a new time (hour, minute, second) on an existing DateTime object. | $d = date_create(); date_time_set($d, 20, 15, 0); echo date_format($d, "H:i:s"); |
date_timestamp_get() |
Retrieves the Unix timestamp (total seconds elapsed since 1970) from a DateTime object. | $d = date_create(); echo date_timestamp_get($d); |
date_timestamp_set() |
Sets the date and time on a DateTime object utilizing a raw Unix timestamp. | $d = date_create(); date_timestamp_set($d, 1770123456); echo date_format($d, "Y-m-d H:i:s"); |
getdate() |
Returns an associative array containing date and time details for the current time or a provided timestamp. | print_r(getdate()); |
gettimeofday() |
Returns the current system time as an array holding seconds, microseconds, and tracking offsets. | print_r(gettimeofday()); |
gmdate() |
Identical to the date() function, but formats the outputted string strictly in the GMT/UTC timezone. | echo gmdate("Y-m-d H:i:s"); |
gmmktime() |
Returns the Unix timestamp for a specific date passed directly in the GMT timezone. | echo gmmktime(0, 0, 0, 6, 5, 2026); |
idate() |
Formats a part of a local time or date and forces the returned value to be a strict integer (int). | echo idate("Y"); // Returns e.g., 2026 as an integer |
localtime() |
Returns the local time broken down into an indexed or associative array (C-style design). | print_r(localtime(time(), true)); |
microtime() |
Returns the current Unix timestamp with microseconds (ideal for benchmarking code speed). | echo microtime(true); |
mktime() |
Calculates and returns the Unix timestamp based on provided local time parameters. | echo mktime(15, 30, 0, 6, 5, 2026); |
strtotime() |
Parses an English text description of a relative time or date into a valid Unix timestamp. | echo strtotime("+1 week 2 days"); |
time() |
Returns the current server time represented as a raw Unix timestamp (total number of seconds). | echo time(); |
timezone_abbreviations_list() |
Returns a highly comprehensive array containing all worldwide timezone abbreviations and their metadata. | print_r(timezone_abbreviations_list()["cet"]); // Example for Central European Time |
timezone_identifiers_list() |
Returns an indexed array holding all officially recognized timezone identifiers across the globe. | print_r(timezone_identifiers_list()); |
timezone_location_get() |
Returns location data (coordinates, country code) associated with a specific DateTimeZone object. | $tz = timezone_open("Europe/Ljubljana"); print_r(timezone_location_get($tz)); |
timezone_name_from_abbr() |
Returns the full timezone name based on its abbreviation prefix and offset parameters. | echo timezone_name_from_abbr("CET"); |
timezone_name_get() |
Returns the name (identifier) of the timezone from a previously opened DateTimeZone object. | $tz = timezone_open("Europe/Ljubljana"); echo timezone_name_get($tz); |
timezone_offset_get() |
Returns the current timezone offset relative to GMT in seconds for the chosen object. | $tz = timezone_open("Europe/Ljubljana"); echo timezone_offset_get($tz, date_create()); |
timezone_open() |
Instantiates and returns a new DateTimeZone object initialized with the provided timezone identifier. | $tz = timezone_open("America/New_York"); var_dump($tz); |
timezone_transitions_get() |
Returns a detailed array log of all timezone transitions (such as winter/summer time shifts). | $tz = timezone_open("Europe/Ljubljana"); print_r(timezone_transitions_get($tz, time(), time()+31536000)); |
timezone_version_get() |
Returns the current version of the official timezone database (timezonedb) configured on the server. | echo timezone_version_get(); |