Given two dates (date1 and date2) and the task is to compare the given dates. Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format.
Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates.
1998-11-24 is latest than 1997-03-26
Method 2: If both of the given dates are in different formats then use strtotime() function to convert the given dates into the corresponding timestamp format and lastly compare these numerical timestamps to get the desired result.
12-03-26 is latest than 2011-10-24
Method 3: Using DateTime class to compare two dates.
2012-11-24 is latest than 2011-03-26
- Return all dates between two dates in an array in PHP
- Comparing float value in PHP
- Sort an array of dates in PHP
- Compare Dates in Java
- How to calculate the difference between two dates in PHP?
- PHP | Number of week days between two dates
- Program to find the number of days between two dates in PHP
- Difference between Search Engine and Web Browser
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.