Hurry Up! Refer new customers to Codentheme and receive  10%  of their each purchase. Become an affiliate now
Understanding Date Format in PHP

Understanding Date Format in PHP

In PHP, working with dates is a common task in many web applications. You can display the current date, format a user-input date, or manipulate dates using PHP’s rich set of functions.

In this blog post, we walk you through different date formats in PHP. We discuss how to format dates, insert date formats, and obtain the current date and time. 

What is the Date Format in PHP?

A date format is a way of converting a date and time into something that can be read by humans. It is commonly used to store dates and times in databases or to display them in web applications.

Learn Different Date Formats in PHP.

PHP provides a range of date formats. PHP developers can use them based on their specific requirements in web applications. The date() function supports a number of different date formats. You can find a list of all the supported date formats in the PHP documentation.

The full list of supported date formats-

  • Day, Month, and Year: This format would display the day of the week, the month, and the year. For example, “Friday, June 23, 2023”.
  • ISO 8601 Date: This format is a standardized date format that is often used in international applications. For example, “2023-06-23”.
  • Short Date: This format is a shorter version of the day, month, and year format. For example, “06/23/23” or “23/06/23”.
  • Full Date: This format includes the day, month, year, and time. For example, “Friday, June 23, 2023 12:34:56”.
  • Time: This format displays the time in a 24-hour format. For example, “12:34:56”.
  • AMPM: This format displays the time in a 12-hour format with AM or PM. For example, “12:34:56 PM”.
  • Timestamp: This format displays the date and time as a Unix timestamp. For example, “1624464000”.

You can also create custom date formats by using a combination of characters. 

For example, the following code would format the date and time in the following format: “2023-06-23 12:34:56”:

$date = date("Y-m-d H:i:s");

How to Format a Date in a PHP?

As you know PHP has a rich set of functions. The date() function helps you format a date in PHP. The date() function accepts two parameters- (a) the format and (b) the optional timestamp.

Note that by default it picks the current time if no timestamp is specified. Let’s look at an example.

$date = date("F j, Y");
echo "Today's date is: " . $date;

Output:

Today's date is: June 23, 2023

In this example, the format parameter “F j, Y” represents the full month name, followed by the day and four-digit year.

How to Insert a Date Format in PHP Web Application?

If you want to insert a date into a database or use it in a query, you’ll need to convert it to a specific format first. And to achieve this the date() function can be combined with other functions such as strtotime() or DateTime to achieve this.

Let’s see an example.

$dateString = "June 23, 2023";
$formattedDate = date("Y-m-d", strtotime($dateString));

// Insert into a database
$query = "INSERT INTO events (event_date) VALUES ('$formattedDate')";

In the above example, the strtotime() function converts the date string to a Unix timestamp, and then uses the date() function to format it into the desired format (“Y-m-d”) suitable for database storage.  

How to Get the Current Date and Time in PHP?

Simply use the date() function. Don’t specify a timestamp to obtain the current date and time.

Here’s an example.

$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");

echo "Today's date is: " . $currentDate . "<br>";
echo "The current time is: " . $currentTime;

Output:

Today's date is: 2023-06-23
The current time is: 12:34:56

In the example, the first date() call retrieves the current date in the “Y-m-d” format, while the second call retrieves the current time in the “H:i:s” format.

Examples of Date Formatting in PHP.

Let’s explore a few more examples to solidify your understanding of date formatting in PHP.

Displaying the Date with Day and Month.

$date = date("l, F j");
echo "Today is: " . $date;

Output:

Today is: Friday, June 23

Displaying the Date with Time.

$date = date("F j, Y H:i:s");
echo "Current date and time: " . $date;

Output:

Current date and time: June 23, 2023 12:34:56

Formatting a Unix Timestamp.

$timestamp = 1624464000; // Unix timestamp for June 24, 2021, 00:00:00
$date = date("Y-m-d", $timestamp);
echo "Formatted date: " . $date;

Output:

Formatted date: 2021-06-24

Conclusion.

We explored date formatting in PHP. We discussed different date formats and learned how to format dates using the date() function.

Additionally, we covered inserting date formats into the database, getting the current date and time, and providing practical examples to reinforce the concepts. With this understanding, you can confidently handle date-related tasks in your PHP applications. Happy Coding!

Related Posts

cURL in PHP: A Comprehensive Tutorial
cURL in PHP: A Comprehensive Tutorial

cURL is a powerful tool that can handle all sorts of data-related tasks in PHP…

Read More
How to Use Try Catch in PHP for Effective Debugging.
How to Use Try Catch in PHP for Effective Debugging.

Programming errors are a part of all software development. PHP is no exception! Handling errors…

Read More