php date compare – How to compare two dates in PHP?

php date compare or Calculate the difference between two dates. Here we have declared two dates date1anddate2 in the same format. $diffrence = $today->diff($date); //enddate is “01-01-2022”

php date compare – Comparing two dates in PHP

PHP DateTime: Create, Compare and Format Dates Easily. i have used a comparison operator(>) to compare the dates.

Method 1: How to compare two dates in PHP

<?php 
  $join_dt = "2022-01-15";
  $end_dt = "2032-12-14";  

  if ($join_dt < $end_dt) 
    echo "$join_dt is less than $end_dt"; 
  else
    echo "$join_dt is greater than $end_dt";
?>

Result

2022-01-15 is less than 2032-12-14

Method 2: How to compare two dates in PHP

<?php 
  $join_dt = "2022-01-15"; 
  $end_dt = "2032-12-14"; 

  $timestamp1 = strtotime($join_dt); 
  $timestamp2 = strtotime($end_dt); 

  if ($timestamp1 > $timestamp2)
    echo "$join_dt is greater than $end_dt";
  else
    echo "$join_dt is less than $end_dt"; 
?>

Comparing Two Dates In PHP Examples

Free Live Chat for Any Issue

Result

2022-01-15 is less than 2032-12-14

Comparing two dates in PHP

php date compare with today

<?php

$join_dt = new DateTime('2032-12-11 12:12:00');
$end_dt = new DateTime('2032-12-13 10:12:00');

if ($join_dt > $end_dt) {
    echo 'join_dt greater than end_dt';
}

if ($join_dt < $end_dt) {
    echo 'join_dt lesser than end_dt';
}

if ($join_dt == $end_dt) {
    echo 'end_dt is equal than join_dt';
}

I hope you get an idea about php date compare.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.