Javatpoint Logo
Javatpoint Logo

PHP compare dates

In this article, we will work on the comparison between the dates in PHP.

PHP compare dates

Comparing the dates in PHP is not a tedious task when the dates are in a similar format, but when we compare the dates in a different format, it becomes difficult. Then we will use a DateTime class, date_diff method, and strtotime function for comparing the dates in php.

What do you mean by strtotime function in PHP?

In PHP, the strtotime() function converts a textual date-time description to a UNIX timestamp. This function accepts a string parameter that represents the description of date-time. We can return the textual date-time in date format using the date() function.

Syntax:

What do you mean by date_diff() function in PHP?

The date_diff() function used in php to return the difference between two DateTime objects.

Syntax:

Let's take various examples of how to compare the dates in PHP.

Example 1:

Explanation:

In the above example, we have created a comparison between the given dates in the same format. In this, we have used a simple comparison operator to compare the dates in php.

Output:

Following is the output of this example:

PHP compare dates

Example 2:

Explanation:

In the above example, we have compared the given dates when both the given dates are in different formats. In this, then use a strtotime() function to convert the given dates into the corresponding timestamp format and after that compare the timestamps values to get the desired result.

Output:

Following is the output of this example:

PHP compare dates

Example 3:

Explanation:

In the above example, we have created a comparison between the given dates. In this, we have used a DateTime class to compare between the two dates in php.

Output:

Following is the output of this example:

PHP compare dates

Example 4:

Explanation:

In the above example, we have created a comparison between the given dates. In this, we use a date_diff() method to create a comparison.

Output:

Following is the output of this example:

PHP compare dates





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA