In PHP, date_format() turns a DateTime or DateTimeImmutable object into a formatted string, such as 2024-01-15 or Monday, January 15, 2024.
date_format() is the procedural alias of DateTime::format() (the method is also available on DateTimeImmutable through DateTimeInterface::format()). It uses the same format letters as date(), but it works on an object and uses the timezone stored inside that object instead of the default timezone.
Syntax
Call date_format() with the values you need. Start with this line.
PHP
echo date_format($date, 'Y-m-d H:i:s');
Call
What it does
date_format($date, 'Y-m-d')
Returns a date such as 2024-01-15.
date_format($date, 'D, d M Y')
Returns a short readable date such as Mon, 15 Jan 2024.
$date->format('H:i')
The object style (DateTime::format) that does the same as date_format().
date_format() is an alias of DateTime::format(). It accepts both DateTime and DateTimeImmutable, since both implement DateTimeInterface. Constants such as DATE_ATOM or DateTimeInterface::RFC2822 give ready made formats for APIs and feeds.
Required. A DateTime or DateTimeImmutable object (anything that implements DateTimeInterface).
format
Required. The format string. It uses the same letters as date(), such as Y, m, d, H, i, s, D, l, M, F and A. Escape letters with a backslash to print them literally.
Return Value
Return Value
Description
string
The formatted date and time, using the timezone stored in the object.
PHP Examples
Example 1 — Common Formats
Create one DateTime from a fixed value and print it in several formats.
PHP
<?php
$d = date_create('2024-01-15 10:30:45');
echo date_format($d, 'Y-m-d'), PHP_EOL;
echo date_format($d, 'H:i:s'), PHP_EOL;
echo date_format($d, 'D, d M Y'), PHP_EOL;
echo date_format($d, 'l jS \o\f F Y, g:i a'), PHP_EOL;
Output
2024-01-15
10:30:45
Mon, 15 Jan 2024
Monday 15th of January 2024, 10:30 am
How It Works
date_create() builds a DateTime for a fixed moment, so the output is always the same.
Y-m-d gives the ISO style date and H:i:s gives the 24 hour time.
D, d M Y prints the short weekday, day, short month name and year.
The last format escapes o and f with backslashes so the word 'of' is printed.
Example 2 — Ready Made Constants and Timestamps
PHP has constants for standard formats. The format U returns the Unix timestamp and e returns the timezone name.
d and m always give two digits, and y gives the last two digits of the year.
j and n give the day and month without leading zeros.
g is the 12 hour clock without a leading zero, and A adds AM or PM.
z counts from zero, so March 5 in a leap year is day 64.
Frequently Asked Questions
It formats a DateTime or DateTimeImmutable object as a string using a format you choose, such as 'Y-m-d H:i:s'.
Yes. date_format() is the procedural alias of DateTime::format(). It also works with DateTimeImmutable objects, which have the same format() method.
date() formats a Unix timestamp using the default timezone. date_format() formats an object and uses the timezone stored in that object. Both use the same format letters.
Put a backslash in front of the letter. In a single quoted PHP string, write '\o\f' to print the word 'of'.
Use DATE_ATOM (Y-m-d\TH:i:sP) or the 'c' format letter for ISO 8601 dates. These formats are easy for other programs and JavaScript to read.