PHP date_format() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

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');
CallWhat 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.

Library declarations
PHP
date_format(DateTimeInterface $object, string $format): string

Parameters

ParameterDescription
objectRequired. A DateTime or DateTimeImmutable object (anything that implements DateTimeInterface).
formatRequired. 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 ValueDescription
stringThe 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;

How It Works

  1. date_create() builds a DateTime for a fixed moment, so the output is always the same.
  2. Y-m-d gives the ISO style date and H:i:s gives the 24 hour time.
  3. D, d M Y prints the short weekday, day, short month name and year.
  4. 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.

PHP
<?php
$d = new DateTimeImmutable('2024-01-15 10:30:00', new DateTimeZone('UTC'));

echo date_format($d, DATE_ATOM), PHP_EOL;
echo date_format($d, DateTimeInterface::RFC2822), PHP_EOL;
echo date_format($d, 'U'), PHP_EOL;
echo date_format($d, 'e'), PHP_EOL;

How It Works

  1. date_format() works with a DateTimeImmutable because it accepts any DateTimeInterface.
  2. DATE_ATOM is a ready made ISO 8601 format that is popular in APIs.
  3. RFC2822 is the format used in email headers.
  4. U gives the Unix timestamp in seconds, and e gives the name of the object's timezone.

Example 3 — Padding and Day Numbers

Some letters add leading zeros and others do not. Compare them using a date with small numbers.

PHP
<?php
$d = date_create('2024-03-05 08:07:09');

echo date_format($d, 'd/m/y'), PHP_EOL;
echo date_format($d, 'j/n/Y'), PHP_EOL;
echo date_format($d, 'g:i:s A'), PHP_EOL;
echo "Day of year: ", date_format($d, 'z'), PHP_EOL;

How It Works

  1. d and m always give two digits, and y gives the last two digits of the year.
  2. j and n give the day and month without leading zeros.
  3. g is the 12 hour clock without a leading zero, and A adds AM or PM.
  4. 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.

Next: date_get_last_errors()

date_get_last_errors() returns the warnings and errors from the last date or time string t.

date_get_last_errors() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful