PHP Date & Time Functions - Part 2
date_sub()
Definition: Subtracts a specified interval from a date.
Syntax: date_sub(datetime, interval)
Example:
$date = date_create("2025-07-23");
date_sub($date, date_interval_create_from_date_string("10 days"));
echo date_format($date, "Y-m-d");
date_modify()
Definition: Modifies the timestamp of a DateTime object.
Syntax: $date->modify(modifier)
Example:
$date = date_create("2025-07-23");
date_modify($date, "+1 month");
echo date_format($date, "Y-m-d");
getdate()
Definition: Returns an associative array containing date information of a timestamp.
Syntax: getdate(timestamp)
Example:
$dateInfo = getdate();
print_r($dateInfo);
localtime()
Definition: Returns an array containing the local time.
Syntax: localtime(timestamp, is_associative)
Example:
print_r(localtime(time(), true));
gettimeofday()
Definition: Returns the current time as an associative array.
Syntax: gettimeofday(return_float)
Example:
print_r(gettimeofday());
date_parse()
Definition: Parses a date/time string into an associative array.
Syntax: date_parse(date_string)
Example:
print_r(date_parse("2025-07-23 12:34:56"));
date_parse_from_format()
Definition: Parses a time string according to a specified format.
Syntax: date_parse_from_format(format, date)
Example:
print_r(date_parse_from_format("Y-m-d", "2025-07-23"));
strtotime()
Definition: Parses an English textual datetime into a Unix timestamp.
Syntax: strtotime(time, now)
Example:
echo date("Y-m-d", strtotime("+1 week"));
strftime()
Definition: Formats a local time/date according to locale settings.
Syntax: strftime(format, timestamp)
Example:
echo strftime("%Y-%m-%d", time());
date_time_set()
Definition: Sets the time of a DateTime object.
Syntax: date_time_set(datetime, hour, minute, second)
Example:
$date = date_create("2025-07-23");
date_time_set($date, 15, 30);
echo date_format($date, "Y-m-d H:i:s");
date_default_timezone_get()
Definition: Returns the default timezone used by all date/time functions.
Syntax: date_default_timezone_get()
Example:
echo date_default_timezone_get();
timezone_open()
Definition: Creates new DateTimeZone object.
Syntax: timezone_open(timezone)
Example:
$tz = timezone_open("Asia/Karachi");
echo timezone_name_get($tz);
timezone_name_get()
Definition: Returns the name of the timezone.
Syntax: timezone_name_get(object)
Example:
$tz = timezone_open("Asia/Karachi");
echo timezone_name_get($tz);
timezone_location_get()
Definition: Returns location information for a timezone.
Syntax: timezone_location_get(object)
Example:
$tz = timezone_open("Asia/Karachi");
print_r(timezone_location_get($tz));
timezone_identifiers_list()
Definition: Returns an indexed array containing all timezone identifiers.
Syntax: timezone_identifiers_list()
Example:
print_r(timezone_identifiers_list());