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:

 $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:

 $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:

 $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

 $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

format('Y-m-d H:i:s');`

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

format('Y-m-d H:i:s');`

DateTime and Timestamps

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

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

Adding or Subtracting Time

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:

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:

diff($updated_at);

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

Comparing DateTimes


 $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.

Leave a Comment