Please wait

Date and Time Formats

PHP provides a variety of tools to work with dates and times, making it easy even for beginners to handle these in a website or application. These functions are simple and versatile to display, manipulate, and handle dates.

The most common function is the date() function. Using the date() function, you can get the current date and time. It has one argument, which is a format for the date.

Formatting Dates

The date() function returns a string formatted according to the given format string. The format You can use various characters to format the date in different ways.

Here are some common format characters and their meanings:

CharacterDescriptionOutputs
YYear, 4 digits2023
yYear, 2 digits23
mMonth, with leading zeros08
nMonth, without leading zeros8
dDay of the month, leading zeros04
jDay of the month, no zeros4
HHour, 24-hour format15
hHour, 12-hour format03
iMinutes30
sSeconds00
AAM or PMPM
aam or pmpm
lFull textual day of the weekFriday
DThree-letter day of the weekFri

An official list of format characters can be found on PHP's documentation page.

Date Components

As you can see when formatting dates and times with the date() function, the format string can consist of various characters to represent different components of the date and time.

Date Components

  • Year: Represented by Y for 4 digits (e.g., 2023) or y for 2 digits (e.g., 23).
  • Month: Represented by m for numeric with leading zeros (e.g., 08) or F for full textual representation (e.g., August).
  • Day of the Month: Represented by d with leading zeros (e.g., 04) or j without leading zeros (e.g., 4).

Time Components

  • Hour: Represented by H for 24-hour format (e.g., 15) or h for 12-hour format (e.g., 03).
  • Minute: Represented by i (e.g., 30).
  • Second: Represented by s (e.g., 05).
  • AM or PM: Represented by A in uppercase (e.g., AM) or a in lowercase (e.g., am).

Weekday Components

  • Day of the Week: Represented by l (lowercase 'L') for the full name (e.g., Friday) or D for a three-letter abbreviation (e.g., Fri).

Timezone Components

  • Timezone: Represented by T to display the timezone abbreviation (e.g., UTC) or e for the timezone identifier (e.g., UTC/GMT).

The combination of these components allows for an incredibly diverse range of date and time formats. By stringing them together in the desired order and format, you can tailor the output to match your specific requirements.

Examples

This table showcases examples of date formats.

FormatDescriptionExample Output
Y-m-dYear-Month-Day with leading zeros2023-08-04
y/m/d2-digit Year/Month/Day23/08/04
F j, YFull textual month, day without zero, 4-digit yearAugust 4, 2023
D M jThree-letter day and month, day without zeroFri Aug 4
lFull textual representation of the dayFriday

Here's a code example:

echo date('Y-m-d') . "<br>";
echo date('y/m/d') . "<br>";
echo date('F j, Y') . "<br>";
echo date('D M j') . "<br>";
echo date('l') . "<br>";

This table showcases examples of time formats.

FormatDescriptionExample Output
H:i:s24-hour, minutes, seconds15:30:00
h:i A12-hour with AM or PM03:30 PM
g:i a12-hour without leading zero, am or pm3:30 pm
h:i:s T12-hour, minutes, seconds, timezone03:30:00 UTC
H:i24-hour, minutes15:30

Here's a code example:

echo date('H:i:s') . "<br>";
echo date('h:i A') . "<br>";
echo date('g:i a') . "<br>";
echo date('h:i:s T') . "<br>";
echo date('H:i') . "<br>";

You may get different results based on when you run these functions.

Escaping Characters

You can escape characters in the date() function. Escaping characters is useful when you want to include literal characters in the output that would otherwise be interpreted as format characters.

For example, if you want to include the letter 'Y' in your date string and not have it interpreted as the four-digit year, you would need to escape it.

You can escape characters by preceding them with a backslash \. Here's an example:

echo date('Y-m-d \Y\e\a\r'); // example output: "2023-08-04 Year"

In this example, the Y is interpreted as the four-digit year, while the \Y is interpreted as a literal 'Y'. The resulting output includes the word "Year" at the end.

While it's useful that you can escape characters in the date() function, you might want to consider appending literal characters with concatenation for readability.

echo date('Y-m-d') . ' Year'; // example output: "2023-08-04 Year"

Key Takeaways

  • PHP's date() function is used to format dates and times, allowing various representations as strings.
  • The date() function accepts a format string containing special characters to represent different parts of the date and time (e.g., Y for the year, m for the month).
  • Characters can be escaped with a backslash in the date() function if you want to include them as literals in the output.

Comments

Please read this before commenting