Current Location: Home> Latest Articles> How to format datetime objects in PHP using date_format

How to format datetime objects in PHP using date_format

gitbox 2025-06-03

In daily PHP development, we often need to format dates and times, such as converting timestamps in the database to "year-month-day" formats, or only displaying specific hours and minutes information. The date_format() function in PHP is one of the tools designed specifically for this, which can help us quickly output dates and times in a custom format.

1. What is the date_format function?

date_format() is a built-in function in PHP that formats a DateTime object to a specified date string.

The basic syntax of a function is as follows:

<code> date_format(DateTime $object, string $format): string </code>
  • $object : An object of type DateTime .

  • $format : The time format to be output, the format rules are consistent with the date() function.

2. Description of common format characters

Here are some commonly used formatting characters:

character meaning Sample output
Y Years with 4 digits 2025
y Years of 2 digits 25
m Digital month (with leading zeros) 06
n Digital Month (no leading zeros) 6
d Day (with leading zero) 03
j Day (no leading zero) 3
H Hours (24-hour system) 14
i minute 08
s Second 55

3. Basic usage examples

Let's first look at a basic example, formatting the current time as "Year-Month-Day Hour: Minute: Seconds":

<code> $now = new DateTime(); echo date_format($now, 'Ymd H:i:s'); </code>

Output example:

 2025-06-03 14:08:55

4. Use in combination with database date

Suppose you fetch a time string '2025-06-03 09:30:00' from the database, and you want to format it as 03/06/2025 :

<code> $dateString = '2025-06-03 09:30:00'; $dateObj = date_create($dateString); echo date_format($dateObj, 'd/m/Y'); </code>

The output is:

 03/06/2025

5. Custom output: only display time

Sometimes you only need to output the time part, such as "09:30 AM":

<code> $date = date_create('2025-06-03 09:30:00'); echo date_format($date, 'h:i A'); </code>

Output:

 09:30 AM

6. Practical skills: display the Chinese format date

To output it in Chinese, such as "June 3, 2025", you can write it like this:

<code> $date = new DateTime(); echo date_format($date, 'Y year n month j day'); </code>

Output:

 2025Year6moon3day

7. Process user-defined input time

If you receive a date string passed by the user, you can also use date_format() to process it. For example, the user submitted a form and entered 2025-12-01 :

<code> $userInput = '2025-12-01'; $date = date_create($userInput); echo date_format($date, 'l, F jS, Y'); </code>

The output is:

 Monday, December 1st, 2025

8. Combined with application examples: Event reminder function

Suppose you are developing a reminder app that wants to show "The event will be held at 9:00 am on June 3, 2025", you can do this:

<code> $eventTime = date_create('2025-06-03 09:00:00'); echo 'Event will be held at ' . date_format($eventTime, 'Y year n month j day A g:i') . '; </code>

Note that where A means "AM/PM", g is an hour without leading zeros.

9. Application scenarios in the project

  • Article release time display

  • User registration time record

  • Date format output in calendar module

  • Format the timestamp in the export report

The figure of date_format() can be seen in these places, which is one of the functions that developers are very worthy of mastery.

10. Summary

date_format() is a simple and powerful tool that is used in conjunction with DateTime objects to achieve flexible and variable date-time formatting output. Instead of memorizing various formats by rote, it is better to practice it a few times and you will know it naturally!

If you are developing a project, such as a system built on gitbox.net , this function can also help you deal with various date display needs and improve the readability and professionalism of the code.

Master date_format() to make your time output clearer and more considerate!