PHP date_format() Tutorial 🎯

beginner
7 min

PHP date_format() Tutorial 🎯

Welcome to our comprehensive guide on the PHP date_format() function! In this lesson, we'll walk through the basics and advanced applications of this powerful tool, helping you create dynamic and date-driven projects. Let's dive in!

What is PHP date_format()? πŸ“

The date_format() function in PHP is used to format dates according to the rules specified. It's a handy function when you need to display dates in a specific format, such as dd-mm-yyyy or dd/mm/yyyy.

Basic Usage πŸ’‘

Before we delve into advanced examples, let's see how to use the date_format() function with a simple date.

php
$date = "2023-03-01"; $formattedDate = date_format($date, "d-m-Y"); echo $formattedDate; // Output: 01-03-2023

In the above example, we've used the date_format() function to transform the date from the "Y-m-d" format to "d-m-Y". We'll discuss date formats and available placeholders in the next sections.

Date Formats πŸ“

PHP provides a set of date formats that can be used with the date_format() function. Here's a list of the most common ones:

| Placeholder | Example | Description | |-------------|-----------------|---------------------------------------------------------------------------| | d | 01 | Day of the month (zero-padded) | | D | Mon | Abbreviated weekday name (e.g., Mon, Tue, Wed) | | j | 1 | Day of the month (zero-padded if day is single-digit) | | l | Monday | Full weekday name | | m | 03 | Month (zero-padded) | | M | Mar | Abbreviated month name (e.g., Jan, Feb, Mar) | | n | 3 | Month (no padding) | | F | March | Full month name | | Y | 2023 | Four-digit year | | y | 23 | Two-digit year | | a | AM | A.M. or P.M. based on the hour | | g | 1 | Hour in 12-hour format (zero-padded) | | G | 23 | Hour in 24-hour format | | h | 1 | Hour in 12-hour format (no padding) | | H | 23 | Hour in 24-hour format (no padding) | | i | 30 | Minutes | | s | 45 | Seconds | | A | AM/PM | A.M. or P.M. based on the hour and locale | | T | 0 | Timezone offset from UTC in hours | | Z | +0000 | Timezone offset from UTC in seconds | | c | 2023-03-01 15:30:00 | Timestamp in Y-m-d H:i:s format | | e | 2 | Day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday) | | O | -04:00 | Timezone offset from UTC in hours and minutes | | w | 6 | Week number of the year (Sunday is the first day of the week) | | W | 10 | Week number of the year (Monday is the first day of the week) | | z | 54 | Day of the year (0 = January 1, 1 = January 2, ..., 365 = December 31) |

Advanced Examples πŸ’‘

Let's create a practical example to showcase the power of the date_format() function. Suppose we're developing a blog and want to display the date of each post according to a custom format.

php
$post_date = "2023-03-01 14:30:00"; $formattedDate = date_format(date_create_from_format("Y-m-d H:i:s", $post_date), "F jS, Y"); echo $formattedDate; // Output: March 1st, 2023

In this example, we've used the date_create_from_format() function to parse the date string, then formatted it using date_format(). This way, we can display the date in a more user-friendly format.

Quick Quiz
Question 1 of 1

Which PHP function is used to format dates according to the rules specified?

We hope you found this tutorial helpful and engaging! As you continue to explore PHP, you'll discover even more ways to leverage the date_format() function in your projects. Happy coding! πŸŽ‰