Categories
PHP

Formatting Date and Time

In this tutorial, we’ll discuss the date(), gmdate() and idate() functions. We’ll use these functions to convert a timestamp to readable date and time format, to change the format of a date or time, and to format the current date and time.

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 :

  1. date() format a Unix timestamp
  2. gmdate() format a GMT/UTC timestamp
  3. idate() format a local time/date as an integer
  4. Date and Time Formatting Characters: represent various date and time symbols.
  5. 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 characterMeaning
a, A“am” or “pm”; “AM” or “PM”
STwo-character English ordinal suffix: “st”, “nd”, “rd”, “th”
d, jDay of the month: with leading zeros: “01”; without: “1”
D, lDay of the week: as three letters: “Mon”; spelled out: “Monday”
M, FMonth: as three letters: “Jan”; spelled out: “January”
m, nMonth: with leading zeros: “01”-“12”; without: “1”-“12”
h, gHour, 12-hour format: with leading zeros: “09”; without: “9”
H, GHour, 24-hour format: with leading zeros: “01”; without “1”
iMinutes:”00″ to “59”
sSeconds: “00” to “59”
Y, yYear: four digits “2002”; two digits “02”
rRFC-2822 formatted date: e.g., “Tue, 29 Jan 2002 09:15:33 +1000” (added in PHP 4.0.4)
wDay of the week as number: “0” (Sunday) to “6” (Saturday)
tDays in the month: “28” to “31”
zDays in the year: “0” to “365”
BSwatch Internet time
LLeap year: “0” for normal year; “1” for leap-year
IDaylight savings time: “0” for standard time; “1” for daylight savings
ODifference to Greenwich Mean Time in hours: “+0200”
TTime zone setting of this machine
ZTime zone offset in seconds: “-43200” to “43200”
USeconds since the epoch: 00:00:00 1/1/1970
Date and Time Formatting Characters

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:

ConstantEquals toOutput example
DATE_ATOMY-m-d\TH:i:sP2022-08-14T05:48:29+02:00
DATE_COOKIEY-m-d\TH:i:sPSunday, 14-Aug-2022 05:48:29 CEST
DATE_ISO8601Y-m-d\TH:i:sP2022-08-14T05:48:29+0200
DATE_RFC822Y-m-d\TH:i:sPSun, 14 Aug 22 05:48:29 +0200
DATE_RFC850Y-m-d\TH:i:sPSunday, 14-Aug-22 05:48:29 CEST
DATE_RFC1036Y-m-d\TH:i:sPSun, 14 Aug 22 05:48:29 +0200
DATE_RFC1123Y-m-d\TH:i:sPSun, 14 Aug 2022 05:48:29 +0200
DATE_RFC7231Y-m-d\TH:i:sPSun, 14 Aug 2022 05:48:29 GMT
DATE_RFC2822Y-m-d\TH:i:sPSun, 14 Aug 2022 05:48:29 +0200
DATE_RFC3339Y-m-d\TH:i:sP2022-08-14T05:48:29+02:00
DATE_RFC3339_EXTENDEDY-m-d\TH:i:sP2022-08-14T05:48:29.000+02:00
DATE_RSSY-m-d\TH:i:sPSun, 14 Aug 2022 05:48:29 +0200
DATE_W3CY-m-d\TH:i:sP2022-08-14T05:48:29+02:00
Formatting DateTime: Predefined Constants

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: