how to compare date in php – Comparing two dates in PHP Examples

how to compare date in php? – we can analyze the dates by simple comparison operator if the given dates are in a similar format. Calculate the difference between two dates in php: Difference of Dates in PHP – Find out how you can get the date/time difference in seconds between two PHP dates.

how to compare date in php? – Comparing two dates in PHP

Case 1:

<?php
   $created_at = "2025-11-24";
   $updated_at = "2026-03-26";
   if ($created_at > $updated_at)
     echo "$created_at is latest than $updated_at";
   else
     echo "$created_at is older than $updated_at";
?>

2026-03-26 is latest than 2025-11-24

Case 2:

<?php
   $created_at = "18-03-22";
   $updated_at = "2017-08-24";
   $curtimestamp1 = strtotime($created_at);
   $curtimestamp2 = strtotime($updated_at);
   if ($curtimestamp1 > $curtimestamp2)
      echo "$created_at is latest than $updated_at";
   else
      echo "$created_at is older than $updated_at";
?>

18-03-22 is latest than 2017-08-24

Case 3:

<?php
   $created_at = new DateTime("18-02-24");
   $updated_at = new DateTime("2026-03-24");
   if ($created_at > $updated_at) {
    echo 'created_at greater than updated_at';
   }
   if ($created_at < $updated_at) {
    echo 'created_at lesser than updated_at';
   }
  if ($created_at == $updated_at) {
    echo 'updated_at is equal than created_at';
   }
?>

created_at lesser than updated_at

compare dates in php

$today = date("Y-m-d");
$expire = $row->expireDate; //from database

$today_time = strtotime($today);
$expire_time = strtotime($expire);

if ($expire_time < $today_time) { /* do Something */ }

php date compare with today

<?php

$created_at = new DateTime('2022-10-11 12:12:00');
$updated_at = new DateTime('2022-10-13 10:12:00');

if ($created_at > $updated_at) {
    echo 'created_at greater than updated_at';
}

if ($created_at < $updated_at) {
    echo 'created_at lesser than updated_at';
}

if ($created_at == $updated_at) {
    echo 'updated_at is equal than created_at';
}

date comparison function in php

Select if(Date('2020-10-01') > Date('2020-11-01'), '1', '2' ) as rslt_date

php compare dates

$created_at = "2021-01-15";
$updated_at = "2021-01-18";

if ($created_at < $updated_at) {
 	echo "$created_at is earlier than $updated_at";
} else {
	echo "$created_at is later than $updated_at";
}

PHP DateTime: Create, Compare and Format Dates Easily

DateTime Instantiation and Formatting

Read Also:  jQuery ajax pagination in php MySQLi

<?php

$dateTime = new DateTime('2021-01-01');
echo $dateTime->format('Y-m-d H:i:s');`

Output: 2021-01-01 00:00:00

<?php

$dateTime = new DateTime();
echo $dateTime->format('Y-m-d H:i:s');`

<?php

$date = new DateTime('2021-05-20', new DateTimeZone('Europe/Berlin'));

DateTime and Timestamps

<?php

$dateTime = new DateTime();
$dateTime->setTimestamp(1271802325);
echo $dateTime->format('Y-m-d H:i:s');

Output: 2022-04-20 22:25:25

Adding or Subtracting Time

<?php

$dateTime = new DateTime('2021-01-01');
$dateTime->modify('+1 day');
echo $dateTime->format('Y-m-d H:i:s');`

Don’t Miss : Comparing two dates in javascript

Interval Between Different Dates

It’s very easy to use DateTime classes to compare dates:

Read Also:  About prevNext - first‹ prevnext ›last

<?php

$created_at = new DateTime('2022-10-11 12:12:00');
$updated_at = new DateTime('2022-10-13 10:12:00');

$interval = $created_at->diff($updated_at);
echo $interval->format('%Y-%m-%d %H:%i:%s');

Output: 00-0-1 22:0:0

If you only want the seconds, hours or day of interval you can write:

<?php

$created_at = new DateTime('2022-10-11 12:12:00');
$updated_at = new DateTime('2022-10-13 10:12:00');

$interval = $created_at->diff($updated_at);

echo $interval->s;
echo $interval->h;
echo $interval->d;

Comparing DateTimes

<?php

$created_at = new DateTime('2022-10-11 12:12:00');
$updated_at = new DateTime('2022-10-13 10:12:00');

if ($created_at > $updated_at) {
    echo 'created_at greater than updated_at';
}

if ($created_at < $updated_at) {
    echo 'created_at lesser than updated_at';
}

if ($created_at == $updated_at) {
    echo 'updated_at is equal than created_at';
}

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