- PHP date() Function
- Definition and Usage
- Syntax
- Parameter Values
- Technical Details
- PHP DateTime
- Introduction to the PHP DateTime class
- Creating a DateTime object from a string
- Comparing two DateTime objects
- Calculating the differences between two DateTime objects
- Adding an interval to a DateTime object
- Summary
PHP date() Function
Format a local date and time and return the formatted date strings:
// Prints the day, date, month, year, time, AM or PM
echo date(«l jS \of F Y h:i:s A»);
?>
Definition and Usage
The date() function formats a local date and time, and returns the formatted date string.
Syntax
Parameter Values
- d — The day of the month (from 01 to 31)
- D — A textual representation of a day (three letters)
- j — The day of the month without leading zeros (1 to 31)
- l (lowercase ‘L’) — A full textual representation of a day
- N — The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)
- S — The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
- w — A numeric representation of the day (0 for Sunday, 6 for Saturday)
- z — The day of the year (from 0 through 365)
- W — The ISO-8601 week number of year (weeks starting on Monday)
- F — A full textual representation of a month (January through December)
- m — A numeric representation of a month (from 01 to 12)
- M — A short textual representation of a month (three letters)
- n — A numeric representation of a month, without leading zeros (1 to 12)
- t — The number of days in the given month
- L — Whether it’s a leap year (1 if it is a leap year, 0 otherwise)
- o — The ISO-8601 year number
- Y — A four digit representation of a year
- y — A two digit representation of a year
- a — Lowercase am or pm
- A — Uppercase AM or PM
- B — Swatch Internet time (000 to 999)
- g — 12-hour format of an hour (1 to 12)
- G — 24-hour format of an hour (0 to 23)
- h — 12-hour format of an hour (01 to 12)
- H — 24-hour format of an hour (00 to 23)
- i — Minutes with leading zeros (00 to 59)
- s — Seconds, with leading zeros (00 to 59)
- u — Microseconds (added in PHP 5.2.2)
- e — The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)
- I (capital i) — Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
- O — Difference to Greenwich time (GMT) in hours (Example: +0100)
- P — Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)
- T — Timezone abbreviations (Examples: EST, MDT)
- Z — Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
- c — The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)
- r — The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
- U — The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
and the following predefined constants can also be used (available since PHP 5.1.0):
- DATE_ATOM — Atom (example: 2013-04-12T15:52:01+00:00)
- DATE_COOKIE — HTTP Cookies (example: Friday, 12-Apr-13 15:52:01 UTC)
- DATE_ISO8601 — ISO-8601 (example: 2013-04-12T15:52:01+0000)
- DATE_RFC822 — RFC 822 (example: Fri, 12 Apr 13 15:52:01 +0000)
- DATE_RFC850 — RFC 850 (example: Friday, 12-Apr-13 15:52:01 UTC)
- DATE_RFC1036 — RFC 1036 (example: Fri, 12 Apr 13 15:52:01 +0000)
- DATE_RFC1123 — RFC 1123 (example: Fri, 12 Apr 2013 15:52:01 +0000)
- DATE_RFC2822 — RFC 2822 (Fri, 12 Apr 2013 15:52:01 +0000)
- DATE_RFC3339 — Same as DATE_ATOM (since PHP 5.1.3)
- DATE_RSS — RSS (Fri, 12 Aug 2013 15:52:01 +0000)
- DATE_W3C — World Wide Web Consortium (example: 2013-04-12T15:52:01+00:00)
Technical Details
Return Value: | Returns a formatted date string on success. FALSE on failure + an E_WARNING |
---|---|
PHP Version: | 4+ |
Changelog: | PHP 5.1.0: Added E_STRICT and E_NOTICE time zone errors. Valid range of timestamp is now from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. Before version 5.1.0 timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows). PHP 5.1.1: Added constants of standard date/time formats that can be used to specify the format parameter |
❮ PHP Date/Time Reference
PHP DateTime
Summary: in this tutorial, you’ll learn how to work with the date and time in an object-oriented way.
Introduction to the PHP DateTime class
PHP provides a set of date and time classes that allow you to work with the date and time in an object-oriented way.
To create a new date and time object, you use the DateTime class. For example:
$datetime = new DateTime(); var_dump($datetime);
Code language: PHP (php)
object(DateTime)#1 (3) ["date"]=> string(26) "2021-07-15 06:30:40.294788" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" >
Code language: PHP (php)
The DateTime object represents the current date and time in the timezone specified in the PHP configuration file ( php.ini )
To set a new timezone, you create a new DateTimeZone object and pass it to the setTimezone() method of the DateTime object:
$datetime = new DateTime(); $timezone = new DateTimeZone('America/Los_Angeles'); $datetime->setTimezone($timezone); var_dump($datetime);
Code language: PHP (php)
object(DateTime)#1 (3) ["date"]=> string(26) "2021-07-14 21:33:27.986925" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "America/Los_Angeles" >
Code language: PHP (php)
In this example, we create a new DateTimeZone object and set it to «America/Los_Angeles» . To get valid timezones supported by PHP, check out the timezone list.
To format a DateTime object, you use the format() method. The format string parameters are the same as those you use for the date() function. For example:
$datetime = new DateTime(); echo $datetime->format('m/d/Y g:i A');
Code language: PHP (php)
07/15/2021 6:38 AM
Code language: PHP (php)
To set a specific date and time, you can pass a date & time string to the DateTime() constructor like this:
$datetime = new DateTime('12/31/2019 12:00 PM'); echo $datetime->format('m/d/Y g:i A');
Code language: PHP (php)
Or you can use the setDate() function to set a date:
$datetime = new DateTime(); $datetime->setDate(2020, 5, 1); echo $datetime->format('m/d/Y g:i A');
Code language: PHP (php)
05/01/2020 6:42 AM
Code language: PHP (php)
The time is derived from the current time. To set the time, you use the setTime() function:
$datetime = new DateTime(); $datetime->setDate(2020, 5, 1); $datetime->setTime(5, 30, 0); echo $datetime->format('m/d/Y g:i A');
Code language: PHP (php)
05/01/2020 5:30 AM
Code language: PHP (php)
Since the setDate() , setTime() , and setTimeZone() method returns the DateTime object, you can chain them like this which is quite convenient.
$datetime = new DateTime(); echo $datetime->setDate(2020, 5, 1) ->setTime(5, 30) ->setTimezone(new DateTimeZone('America/New_York')) ->format('m/d/Y g:i A');
Code language: PHP (php)
04/30/2020 11:30 PM
Code language: PHP (php)
Creating a DateTime object from a string
When you pass the date string ’06/08/2021′ to the DateTime() constructor or setDate() function, PHP interprets it as m/d/Y . For example:
$datetime = new DateTime('06/08/2021'); echo $datetime->format('F jS, Y');
Code language: PHP (php)
June 8th, 2021
Code language: PHP (php)
If you want to pass it as August 6th, 2021, you need to use the – or . instead of /. For example:
$datetime = new DateTime('06-08-2021'); echo $datetime->format('F jS, Y');
Code language: PHP (php)
August 6th, 2021
Code language: PHP (php)
However, if you want to parse the date string ’06/08/2021′ as d/m/Y, you need to replace the / with – or . manually:
$ds = '06/08/2021'; $datetime = new DateTime(str_replace('/', '-', $ds)); echo $datetime->format('F jS, Y');
Code language: PHP (php)
August 6th, 2021
Code language: PHP (php)
A better way to do it is to use the createFromFormat() static method of the DateTime object:
$ds = '06/08/2021'; $datetime = DateTime::createFromFormat('d/m/Y', $ds); echo $datetime->format('F jS, Y');
Code language: PHP (php)
In this example, we pass the date format as the first argument and the date string as the second argument.
Note that when you pass a date string without the time, the DateTime() constructor uses midnight time. However, the createFromFormat() method uses the current time.
Comparing two DateTime objects
PHP allows you to compare two DateTime objects using the comparison operators including >, >=, . For example:
$datetime1 = new DateTime('01/01/2021 10:00 AM'); $datetime2 = new DateTime('01/01/2021 09:00 AM'); var_dump($datetime1 < $datetime2); // false var_dump($datetime1 > $datetime2); // true var_dump($datetime1 == $datetime2); // false var_dump($datetime1 $datetime2); // 1
Code language: PHP (php)
Calculating the differences between two DateTime objects
The diff() method of the DateTime() object returns the difference between two DateTime() objects as a DateInterval object. For example:
$dob = new DateTime('01/01/1990'); $to_date = new DateTime('07/15/2021'); $age = $to_date->diff($dob); var_dump($age);
Code language: PHP (php)
object(DateInterval)#3 (16) ["y"]=> int(31) ["m"]=> int(6) ["d"]=> int(14) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(1) ["days"]=> int(11518) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) >
Code language: PHP (php)
The DateInterval represents the differences between two dates in the year, month, day, hour, etc. To format the difference, you use the DateInterval ‘s format. For example:
$dob = new DateTime('01/01/1990'); $to_date = new DateTime('07/15/2021'); echo $to_date->diff($dob)->format('%Y years, %m months, %d days');
Code language: PHP (php)
31 years, 6 months, 14 days
Code language: PHP (php)
Adding an interval to a DateTime object
To add an interval to date, you create a new DateInterval object and pass it to the add() method. The following example adds 1 year 2 months to the date 01/01/2021 :
$datetime = new DateTime('01/01/2021'); $datetime->add(new DateInterval('P1Y2M')); echo $datetime->format('m/d/Y');
Code language: PHP (php)
03/01/2022
Code language: PHP (php)
To format a date interval, you use the date interval format strings.
To subtract an interval from a DateTime object, you create a negative interval and use the add() method.
Summary
- Use the DateTime class to work with the date and time.
- Use the DateTimeZone class to work with time zones.
- Use the comparison operators to compare two DateTime objects.
- Use the diff() method to calculate the difference between to DateTime objects.
- Use the DateInterval class to represent a date and time interval.
- Use the add() method to add an interval to or subtract an interval from a DateTime object.