While the Unix timestamp is programmatically useful, it isn’t a convenient display format. The date( )
and gmdate( )
functions return a human-readable formatted date and time. The idate()
function returns an integer i.e. 12 for the month of December :
date()
format a Unix timestampgmdate()
format a GMT/UTC timestampidate()
format a local time/date as an integer- Date and Time Formatting Characters: represent various date and time symbols.
- Formatting Date and Time with PHP’s Predefined Constants
date()
<?php //Syntax date(string $format, ?int $timestamp = null): string
The date()
function takes two parameters: $format
and optional $timestamp
. The format of the returned string is determined by the format parameter. A predetermined date can be formatted by passing in the optional timestamp parameter otherwise it formats the current time (value of time() function).
The function date()
is very powerful and offers a broad range of ways to use it. The format string uses the formatting characters listed in the following table to display various symbols or characteristics of the timestamp. The following examples show various combinations.
Example: Formatting current date and time
<?php echo date('m/d/y') . '<br>' # 08/04/22 . date('m/d/Y') . '<br>' # 08/04/2022 . date('d m Y') . '<br>' # 08/04/2022 . date('d M y') . '<br>' # 04 Aug 22 . date('d M Y') . '<br>' # 04 Aug 2022 . date('H:i:s a'). '<br>'# 07:48:01 am . date('H:i:s A'). '<br>'# 07:48:01 AM . date('d M y H:i:s A'); # 04 Aug 22 07:48:01 AM
Example: Write inside the date() function
If you want to write something within the date() function, use the backslash character to escape each alphabet:
<?php echo date ('\D\a\y: d, \M\o\n\t\h: M, \Y\e\a\r: Y'); # Prints: Day: 04, Month: Aug, Year: 2022
Example: Convert a timestamp to readable date and time format
If you’ve a timestamp stored in the database or generated a timestamp with mktime() or strtotime() function, you can convert that to a readable date and time format with date()
function:
<?php $ts = 1659514532; echo date('d M Y - H:i:s', $ts).'<br>'; # Prints: 03 Aug 2022 - 10:15:32 echo date('m-d-Y', $ts); # Prints: 08-03-2022
Example: Change the format of a date or time
To change the format of a date, first, convert it to Unix timestamp and then use date()
function to format that timestamp, see the following code:
<?php $dob = 'December 13, 99'; $ts = strtotime($dob); echo date('m-d-Y', $ts); #Prints: 12-13-1999
Example: Unix timestamp to MySQL date format
<?php $date = date('Y-m-d H:i:s');
gmdate()
<?php //Syntax gmdate(string $format, ?int $timestamp = null): string
The gmdate()
function works similar to the date()
function except that the string returned is Greenwich Mean Time (GMT).
<?php echo gmdate('d M Y H:i:s').'<br>'; # 04 Aug 2022 06:50:03 echo date ('d M Y H:i:s').'<br>'; # 04 Aug 2022 08:50:03 // using timestamp echo gmdate('d M Y H:i:s', 0).'<br>'; # Prints: 01 Jan 1970 00:00:00 echo date('d M Y H:i:s', 0); # Prints: 01 Jan 1970 01:00:00
idate()
<?php //Syntax idate(string $format, ?int $timestamp = null): int|false
The idate()
function takes two parameters: $format
and optional $timestamp
. If the timestamp parameter is null or not provided, the current date and time are used (default to time() ). The format of the returned string is determined by the format parameter. Unlike the date()
function, the idate()
accepts just one char in the format parameter.
The idate()
function returns an integer, it omitted 0 if the returned value starts with 0. For example, 4 will return if you expect the 04.
<?php echo idate('Y'); #Prints: 2022 // Current month is August, // returns 8 instead of 08 echo idate('m'); #Prints: 8
Note: This function can not use all formatting characters, it uses only those characters that return the integer value.
Formatting characters
Table: Formatting characters that represent various date and time components.
Formatting character | Meaning |
---|---|
a, A | “am” or “pm”; “AM” or “PM” |
S | Two-character English ordinal suffix: “st”, “nd”, “rd”, “th” |
d, j | Day of the month: with leading zeros: “01”; without: “1” |
D, l | Day of the week: as three letters: “Mon”; spelled out: “Monday” |
M, F | Month: as three letters: “Jan”; spelled out: “January” |
m, n | Month: with leading zeros: “01”-“12”; without: “1”-“12” |
h, g | Hour, 12-hour format: with leading zeros: “09”; without: “9” |
H, G | Hour, 24-hour format: with leading zeros: “01”; without “1” |
i | Minutes:”00″ to “59” |
s | Seconds: “00” to “59” |
Y, y | Year: four digits “2002”; two digits “02” |
r | RFC-2822 formatted date: e.g., “Tue, 29 Jan 2002 09:15:33 +1000” (added in PHP 4.0.4) |
w | Day of the week as number: “0” (Sunday) to “6” (Saturday) |
t | Days in the month: “28” to “31” |
z | Days in the year: “0” to “365” |
B | Swatch Internet time |
L | Leap year: “0” for normal year; “1” for leap-year |
I | Daylight savings time: “0” for standard time; “1” for daylight savings |
O | Difference to Greenwich Mean Time in hours: “+0200” |
T | Time zone setting of this machine |
Z | Time zone offset in seconds: “-43200” to “43200” |
U | Seconds since the epoch: 00:00:00 1/1/1970 |
See the complete list on https://www.php.net/manual/en/datetime.format.php.
Formatting Date and Time with PHP’s Predefined Constants
Common predefined constants that you can use to represent your date and time format:
Constant | Equals to | Output example |
---|---|---|
DATE_ATOM | Y-m-d\TH:i:sP | 2022-08-14T05:48:29+02:00 |
DATE_COOKIE | Y-m-d\TH:i:sP | Sunday, 14-Aug-2022 05:48:29 CEST |
DATE_ISO8601 | Y-m-d\TH:i:sP | 2022-08-14T05:48:29+0200 |
DATE_RFC822 | Y-m-d\TH:i:sP | Sun, 14 Aug 22 05:48:29 +0200 |
DATE_RFC850 | Y-m-d\TH:i:sP | Sunday, 14-Aug-22 05:48:29 CEST |
DATE_RFC1036 | Y-m-d\TH:i:sP | Sun, 14 Aug 22 05:48:29 +0200 |
DATE_RFC1123 | Y-m-d\TH:i:sP | Sun, 14 Aug 2022 05:48:29 +0200 |
DATE_RFC7231 | Y-m-d\TH:i:sP | Sun, 14 Aug 2022 05:48:29 GMT |
DATE_RFC2822 | Y-m-d\TH:i:sP | Sun, 14 Aug 2022 05:48:29 +0200 |
DATE_RFC3339 | Y-m-d\TH:i:sP | 2022-08-14T05:48:29+02:00 |
DATE_RFC3339_EXTENDED | Y-m-d\TH:i:sP | 2022-08-14T05:48:29.000+02:00 |
DATE_RSS | Y-m-d\TH:i:sP | Sun, 14 Aug 2022 05:48:29 +0200 |
DATE_W3C | Y-m-d\TH:i:sP | 2022-08-14T05:48:29+02:00 |
See how these constants works on your code:
<?php echo date(DATE_ATOM) . '<br>'; # 2022-08-14T06:04:07+02:00 echo date(DATE_RSS) . '<br>'; # Sun, 14 Aug 2022 06:04:07 +0200 echo date(DATE_W3C) ; # 2022-08-14T06:04:07+02:00
For more information, visit https://www.php.net/manual/class.datetimeinterface.php.
The Date and Time Tutorials:
- PHP DateTime Class
- PHP DateTimeZone Class – times in different countries
- PHP DateInterval Class – adds or subtracts dates/times
- PHP DatePeriod Class – generates date or time ranges
- PHP Validating Age and Date of Birth
- Sunset, Sunrise, Transit, and Twilight
- Localizing Dates
- Localizing Dates with IntlDateFormatter Class