
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Comparison of dates in PHP
Matching two dates in PHP is quite smooth when both the dates are in a similar format but php failed to analyze when the two dates are in an unrelated format. In this article, we will discuss different cases of date comparison in PHP. We will figure out how to utilize DateTime class,strtotime() in comparing dates.
Case 1:
we can analyze the dates by simple comparison operator if the given dates are in a similar format.
<?php $date1 = "2018-11-24"; $date2 = "2019-03-26"; if ($date1 > $date2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?>
Output:
2019-03-26 is latest than 2018-11-24
Explanation:
Here we have declared two dates $date1 and $date2 in the same format. So we have used a comparison operator(>) to compare the dates.
Case 2:
If the given dates are in various formats at that point we can utilize the strtotime() function to convert the given dates into the UNIX timestamp format and analyze these numerical timestamps to get the expected result.
Example:
<?php $date1 = "18-03-22"; $date2 = "2017-08-24"; $curtimestamp1 = strtotime($date1); $curtimestamp2 = strtotime($date2); if ($curtimestamp1 > $curtimestamp2) echo "$date1 is latest than $date2"; else echo "$date1 is older than $date2"; ?>
Output:
18-03-22 is latest than 2017-08-24
Explanation:
In this example, we have two Dates which are in a different format. So we have used predefined function strtotime() convert them into numeric UNIX timestamp, then to compare those timestamps we use different comparison operators to get the desired result.
Case 3:
Comparing two dates by creating the object of the DateTime class.
Example:
<?php $date1 = new DateTime("18-02-24"); $date2 = new DateTime("2019-03-24"); if ($date1 > $date2) { echo 'datetime1 greater than datetime2'; } if ($date1 < $date2) { echo 'datetime1 lesser than datetime2'; } if ($date1 == $date2) { echo 'datetime2 is equal than datetime1'; } ?>
Output:
datetime1 lesser than datetime2
Explanation:
In this example, we created two DateTime objects. In order to compare those two dates, we use different comparison operators to get the desired result.
- Related Articles
- Sort an array of dates in PHP
- Comparing two dates in PHP
- Comparison of floating point values in PHP.\n
- Return all dates between two dates in an array in PHP
- Comparison between "&&" and "AND" operator in PHP.
- MySQL date comparison to fetch dates between a given range?
- PHP program to compare two dates
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- PHP program to sort dates given in the form of an array
- PHP program to find the total number of date between any two given dates
- Comparison of Float in Java
- Degree of Comparison
- Perform element-wise comparison of two string arrays using a comparison operator in Numpy
- Comparison of CSS versions
- Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?
