- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to compare two dates
To compare two dates in PHP, the code is as follows −
Example
<?php $date_1 = new DateTime("2020-11-22"); $date_2 = new DateTime("2011-11-22"); if ($date_1 > $date_2) echo $date_1->format("Y-m-d") . " is later than ". $date_2->format("Y-m-d"); else echo $date_1->format("Y-m-d") . " is before " . $date_2->format("Y-m-d"); ?>
Output
2020-11-22 is later than 2011-11-22
Two dates of ‘DateTime’ format are generated and checked to see which one is sooner or later. If the first date is later, relevant message is printed on the console. Otherwise, a message indicating that the first date is sooner than the second date is printed on the console.
- Related Articles
- How to compare two Dates in C#?
- How to compare two dates in Java?
- How to compare two dates with JavaScript?
- Comparing two dates in PHP
- How to compare two dates along with time in Java?
- How to compare two dates in String format in Java?
- PHP program to compare float values
- PHP program to find the total number of date between any two given dates
- Return all dates between two dates in an array in PHP
- How to compare dates in JavaScript?
- Compare Dates in Java
- Java Program to compare dates if a date is after another date
- Java Program to compare dates if a date is before another date
- Java Program to compare two sets
- Java Program to Compare Two Strings

Advertisements