0% found this document useful (0 votes)
3 views2 pages

PHP Date Functions Part1

The document provides an overview of various PHP date and time functions, including their definitions, syntax, and examples. Key functions include date(), date_default_timezone_set(), mktime(), and date_create(), among others. Each function is accompanied by a brief explanation and a sample code snippet for practical understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

PHP Date Functions Part1

The document provides an overview of various PHP date and time functions, including their definitions, syntax, and examples. Key functions include date(), date_default_timezone_set(), mktime(), and date_create(), among others. Each function is accompanied by a brief explanation and a sample code snippet for practical understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP Date & Time Functions - Definitions, Syntax & Examples

date()
Definition: Formats a local date and time.
Syntax: string date(string $format, int $timestamp = time())
Example: echo date('Y-m-d H:i:s');

date_default_timezone_set()
Definition: Sets the default timezone used by all date/time functions.
Syntax: bool date_default_timezone_set(string $timezone_identifier)
Example: date_default_timezone_set('Asia/Karachi');

mktime()
Definition: Gets Unix timestamp for a date.
Syntax: int mktime(hour, minute, second, month, day, year)
Example: echo mktime(0, 0, 0, 7, 1, 2025);

gmmktime()
Definition: Gets Unix timestamp for a GMT date.
Syntax: int gmmktime(hour, minute, second, month, day, year)
Example: echo gmmktime(0, 0, 0, 7, 1, 2025);

date_create()
Definition: Returns a new DateTime object.
Syntax: DateTime date_create(string $datetime = 'now')
Example: $date = date_create('2025-07-23');

date_format()
Definition: Returns date formatted according to given format.
Syntax: string date_format(DateTime $object, string $format)
Example: echo date_format($date, 'Y-m-d');

checkdate()
Definition: Validates a Gregorian date.
Syntax: bool checkdate(int $month, int $day, int $year)
Example: var_dump(checkdate(2, 29, 2024));

date_diff()
Definition: Returns difference between two dates.
Syntax: DateInterval date_diff(DateTime $datetime1, DateTime $datetime2)
Example: $diff = date_diff(date_create('2025-01-01'), date_create('2025-07-01'));

date_add()
Definition: Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object.
Syntax: DateTime date_add(DateTime $object, DateInterval $interval)
Example: $date = date_create(); date_add($date, date_interval_create_from_date_string('10 days'));

date_sub()
Definition: Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object.
Syntax: DateTime date_sub(DateTime $object, DateInterval $interval)
Example: $date = date_create(); date_sub($date, date_interval_create_from_date_string('5 days'));

You might also like