W3docs

date_format()

Date and time are crucial components in many programming languages, including PHP. Understanding how to format dates and times is essential for any developer.

date_format()

Introduction

Date and time are crucial components in many programming languages, including PHP. Understanding how to format dates and times is essential for any developer. In this article, we will discuss the date_format() function in PHP, a procedural wrapper that allows developers to format dates and times in a variety of ways.

What is the date_format() function?

The date_format() function in PHP is used to format dates and times in a specific way. It takes two arguments: the first argument must be a DateTime object instance, and the second argument is a string that specifies the desired date format. Note that date_format() is a procedural alias for the DateTime::format() method.

How to use the date_format() function

To use the date_format() function, you first need to create a date object. You can create a date object using the DateTime class, which is built into PHP. Here's an example:

$date = new DateTime('2023-03-03');

This creates a new date object set to March 3, 2023, at midnight. By default, DateTime uses the server's timezone. For predictable results, it's best practice to explicitly set the timezone:

$date = new DateTime('2023-03-03', new DateTimeZone('UTC'));

You can then use the date_format() function to format the date in any way you like. Here's an example:

<?php

echo date_format($date, 'Y-m-d');

This will output the date in the format YYYY-MM-DD.

For modern PHP applications, consider using DateTimeImmutable instead. It prevents accidental modifications to the original date object, which is a common source of bugs in date handling.

Common date formats

There are many different ways to format dates using the date_format() function. Here are some of the most common formats:

  • Y-m-d: The date in YYYY-MM-DD format.
  • d/m/Y: The date in DD/MM/YYYY format.
  • m/d/Y: The date in MM/DD/YYYY format.
  • H:i:s: The time in 24-hour format (e.g., 13:00:00).
  • h:i:s a: The time in 12-hour format (e.g., 01:00:00 am).

Custom date formats

In addition to the common formats, you can also create your own custom date formats using the date_format() function. Here are some of the most commonly used format characters:

  • Y: The year in four digits (e.g., 2023).
  • y: The year in two digits (e.g., 23).
  • m: The month as a two-digit number (e.g., 03).
  • M: The abbreviated month name (e.g., Mar).
  • F: The full month name (e.g., March).
  • d: The day of the month as a two-digit number (e.g., 03).
  • D: The abbreviated day name (e.g., Fri).
  • l: The full day name (e.g., Friday).
  • j: The day of the month without leading zeros (e.g., 3).
  • S: English ordinal suffix for the day of the month (e.g., st, nd, rd, th).
  • H: The hour in 24-hour format (e.g., 13).
  • h: The hour in 12-hour format (e.g., 01).
  • i: The minutes as a two-digit number (e.g., 05).
  • s: The seconds as a two-digit number (e.g., 30).
  • a: am or pm.

Here's an example of a custom date format:

<?php

echo date_format($date, 'l, F jS Y, h:i:s a');

This will output the date and time in the format Friday, March 3rd 2023, 12:00:00 am.

Conclusion

The date_format() function in PHP provides a straightforward way to format dates and times for display. By understanding the available format characters and best practices like explicit timezone handling and using DateTimeImmutable, developers can ensure accurate and predictable date output in their applications.

Practice

Practice

What are the parameters used to format a date in PHP according to the content of the specified URL?