DateTime compare php code example
Example 1: php compare two dates
$today = date("Y-m-d");
$expire = $row->expireDate;
$today_time = strtotime($today);
$expire_time = strtotime($expire);
if ($expire_time < $today_time) { }
Example 2: php DateTime comparation
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
var_dump($date1 == $date2);
var_dump($date1 < $date2);
var_dump($date1 > $date2);
?>
Example 3: php DateTime comparation
dev:~# php
<?php
date_default_timezone_set('Europe/London');
$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
?>
bool(false)
bool(true)
bool(false)
dev:~# php -v
PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 20:09:03)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
dev:~#
Example 4: date comparison function in php
Select if(Date('2020-10-01') > Date('2020-11-01'), '1', '2' ) as rslt_date