PHP date() Function

What is PHP date() Function?

If you want to format a specific time to a human readable format, use PHP date() function. Example, 5 January 2024, 12/27/2023, 7:!5 am etc. The function actually takes a timestamp and converts it to your desirable date and time format. Now, you may ask what the timestamp is.

Timestamp: Timestamp of a specific time is the number of second from 1st January 1970 00:00:00 Greenwich Mean Time (GMT) to that specific time. 1st January 1970 00:00:00 Greenwich Mean Time (GMT) is known as Unix Epoch.

So, from a timestamp (which is just a number of some seconds) we human can’t read a date. We need to convert this seconds to a date and time format. And PHP date() function helps us to accomplish this.


date(format, timestamp)


format (required): This is a string. It is the format how you want the date to be displayed.

The PHP date() function uses some English alphabet letters to construct different parts of a date and time. When using in the “format” parameter, they carry special meaning. Check the “Formatting letters / Characters” table below for the full character list with their meaning and examples. Check example 1 to see the formatting characters in action.

timestamp (optional): This is an integer which is an UNIX timestamp. It could indicate any time in the past or in the future. Check example 2. By default, the function uses local time as timestamp. That means, if you don’t mention this parameter, the function uses the local time as the. You can get the current local timestamp from time() function. timestamp Check example 3.

Table: Formatting Letters / Characters

Dayd2 digits day of the months with leading zero.01, 02, ….31
D3 letters abbreviated day name of the week.Sun, Mon, …Sat
jDay of the months without leading zero.1, 2, …..31
l (lowercase “L”)Full day name of the week.Sunday, …Saturday
NISO 8601 numeric day of the week where 1 indicates to Monday while 7 to Sunday.1 (for Monday), 5 (Friday), …
S (capital)2 characters English ordinal suffix for the day of the month. It fits well after day of the month like 1st , 3rd, nd, rd, th, ….
w (lowercase)Numeric day of the week where 0 indicates Sunday and 6 indicates Saturday0 (for Sunday), 5 (Friday), …
z (lowercase)Day of the year without leading zero.1, 2, 3, … 365
WeekWISO 8601 week number of the year. A week starts on Monday. First week of a year starts on first Monday of the January. The character “W” indicates in which week a timestamp is in.52 (52th week of the year)
MonthFFull name of the month.January, February, …
m2 digits month of the year with leading zero.01 (for January), 02, ….12 (for December)
M3 letters abbreviated month of the year.Jan, Feb, …Dec
nName of the month without leading zero.1,2,…..12
tNumber of days of a month.It could be 28 to 31
YearLWhether it is leap year or not.1 (for leap year), 0 (for not leap year)
o (lowercase)ISO 8601 year number based on week-number. Same as Y except if the ISO week number is part of the previous or next year, it will display that year. It may happen in the beginning or in the end of the year,2024, 1990, etc.
XAn at least 4 digits of a year. In front of the year – is added if it is BCE and + if it is CE.-0004, +0579, …
x (lowercase)An at least 4 digits of a year. In front of the year – is added if it is BCE and + if it is 10000 or beyond.-1000, 0900, +10001
YA 4 digits year.2024, 1999, 0109 etc.
y (lowercase)A 2 digit year.24, 99, 09
TimeaLowercase Ante-meridiem and, pm
AUppercase Ante-meridiem and Post-meridiem.AM, PM
BSwatch internet time. The character “B” the Swatch internet time of the provided timestamp.000, 001, … 999
g12 hours format of an hour without leading zeros.1 to 12
G24 hours format of an hour without leading zeros.0 to 23
h12 hours format of an hour with leading zeros.01 to 12
H24 hours format of an hour with leading zeros.00 to 23
iMinutes with leading zeros.00 to 59
s (lowercase)Seconds with leading zeros.00 to 59
u (lowercase)Microseconds. 1,000,000 of a second.100,000, 999,999
v (lowercase)Milliseconds. 1,000 of a second.123, 999
TimezoneeTimezone.GMT, UTC…
I (capital)Whether it is in daylight saving time or not. 1 if it is in daylight saving time 0 otherwise.1 or 0.
O (capital)Time difference from the specific time to the GMT without color between hours and minutes+0100, -800 etc.
P (capital)Time difference from the specific time to the GMT with color between hours and minutes.-08:00 etc.
p (lowercase)Time difference from the specific time to the GMT with color between hours and minutes. It shows z instead of +0:00 for UTC time.Z, +01:00 etc.
TTimezone abbreviation.HKT, MSK etc.
Z (capital)Timezone offset (difference in hours and minutes between a timestamp’s  timezone and UTC). For timezones that are west of UTC are negative and for timezones that are east of UTC are positive.-43200 through 50400
Full Date/Time
cThe ISO 8601 date2023-12-27T11:44:39+00:00
rThe RFC 2822/ RFC 5322 formatted dateWed, 27 Dec 2023 11:44:39 +0000
U (capital)The seconds since UNIX Epoch (January 1 1970 00:00:00 GMT)1703677738

Return Values:

The formatted date of the timestamp –

  • specified in the second parameter, or
  • of the local time if the second parameter is not specified.


Example 1:

echo date("m-j-Y g:i:s a");
echo "<br />";
echo date("d F Y g:i A");
echo "<br />";
echo date("dS F Y");
echo "<br />";
echo "We're in the week: " . date("W");
echo "<br />";
echo date("Y-m-d");
echo "<br />";


12-28-2023 8:07:51 am
28 December 2023 8:07 AM
28th December 2023
We’re in the week: 52

Example 2:

echo date("m/j/Y g:i:s a", 1703728397);


12/28/2023 7:53:17 am


The date and time is retrieved from the timestamp provided in the second parameter.

Exercise 3:

echo date("\D\a\\t\\e: m-d-Y, \T\i\m\\e: g:i:s a");


Date: 12-28-2023, Time: 8:07:51 am


Characters inside the date() function are escaped with the \ character so that they don’t conflict with the regular formatting characters.

Exercise 4:

echo date("m-j-Y g:i:s a");


12-28-2023 2:31:57 am


After setting the timezone as UTC, the date() function in the next line displaying the date and time is showing according to the UTC time zone.

Practical Usages of date() Function:

We use date and time everywhere.  Few real life examples are-

  • To display postdate or update date of an article.
  • To create a date and time before adding a new entry in the database table.
  • To control different functionalities of an event like displaying the discount code in the black Friday only etc.

Notes on date() Function:

  • You can’t specify time zone using date() function. For this, use PHP DateTime class.
  • By default, date() function uses server’s local time zone. If you want to work on a different time zone, use date_default_timezone_set() function to set another time zone.
  • The timestamp parameter is useful for finding specific point of time in the past or in the future. Check example
  • Other than the above formatting characters, following characters are also used between the characters above to add extra formatting to make it more readable. Check any example.
  • As part of the date, you can use letters inside date() function. But, to differ these characters from the formatting characters, you’ll escape it by adding a backslashes(\) in front of each character. Check example 3.
  • If your server is in different timezone than yours, you’ll get a different time. To get the current time, set your PHP script for your desire timezone with the date_default_timezone_set() function. After this, you’ll get date and time according to this timezone. Check example 4.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP date() Function

The date() function may be the most commonly used built-in PHP date and time function. This function is an indispensable part of each PHP script that deals with date and time. The date() function helps you to construct a date according to your requirements.