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:
Character | Description | Outputs |
---|---|---|
Y | Year, 4 digits | 2023 |
y | Year, 2 digits | 23 |
m | Month, with leading zeros | 08 |
n | Month, without leading zeros | 8 |
d | Day of the month, leading zeros | 04 |
j | Day of the month, no zeros | 4 |
H | Hour, 24-hour format | 15 |
h | Hour, 12-hour format | 03 |
i | Minutes | 30 |
s | Seconds | 00 |
A | AM or PM | PM |
a | am or pm | pm |
l | Full textual day of the week | Friday |
D | Three-letter day of the week | Fri |
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) ory
for 2 digits (e.g., 23). - Month: Represented by
m
for numeric with leading zeros (e.g., 08) orF
for full textual representation (e.g., August). - Day of the Month: Represented by
d
with leading zeros (e.g., 04) orj
without leading zeros (e.g., 4).
Time Components
- Hour: Represented by
H
for 24-hour format (e.g., 15) orh
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) ora
in lowercase (e.g., am).
Weekday Components
- Day of the Week: Represented by
l
(lowercase 'L') for the full name (e.g., Friday) orD
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.
Format | Description | Example Output |
---|---|---|
Y-m-d | Year-Month-Day with leading zeros | 2023-08-04 |
y/m/d | 2-digit Year/Month/Day | 23/08/04 |
F j, Y | Full textual month, day without zero, 4-digit year | August 4, 2023 |
D M j | Three-letter day and month, day without zero | Fri Aug 4 |
l | Full textual representation of the day | Friday |
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.
Format | Description | Example Output |
---|---|---|
H:i:s | 24-hour, minutes, seconds | 15:30:00 |
h:i A | 12-hour with AM or PM | 03:30 PM |
g:i a | 12-hour without leading zero, am or pm | 3:30 pm |
h:i:s T | 12-hour, minutes, seconds, timezone | 03:30:00 UTC |
H:i | 24-hour, minutes | 15: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.