
- 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
Comparing two dates in PHP
To compare two dates in PHP, the code is as follows. Here, we have used the equality operator to compare dates −
Example
<?php $dateOne = "2019-10-30"; $dateTwo = "2019-10-30"; echo "Date1 = $dateOne"; echo "
Date2 = $dateTwo"; if ($dateOne == $dateTwo) echo "
Both the dates are equal!"; else echo "Both the dates are different!"; ?>
Output
This will produce the following output−
Date1 = 2019-10-30 Date2 = 2019-10-30 Both the dates are equal!
Example
Let us now see another example−
<?php $dateOne = "2019-11-08"; $dateTwo = "2018-08-10"; echo "Date1 = $dateOne"; echo "
Date2 = $dateTwo"; if ($dateOne < $dateTwo) echo "
DateTwo is the latest date!"; else echo "
DateOne is the latest date!"; ?>
Output
This will produce the following output−
Date1 = 2019-11-08 Date2 = 2018-08-10 DateOne is the latest date!
- Related Articles
- Comparing dates in Python
- Comparing dates using C#
- Return all dates between two dates in an array in PHP
- PHP program to compare two dates
- PHP Comparing Objects
- Comparing float value in PHP
- Comparing dates in MySQL ignoring time portion of a DateTime field?
- Comparison of dates in PHP
- Comparing two strings in MySQL?
- Comparing two strings in C++
- Sort an array of dates in PHP
- Comparing two Strings lexicographically in Java \n
- How to list all dates between two dates in Excel?
- Comparing integers by taking two numbers in JavaScript
- Comparing corresponding values of two arrays in JavaScript

Advertisements