在 PHP 中比较两个日期
要在 PHP 中比较两个日期,代码如下。在这里,我们使用相等运算符来比较日期 -
示例
<?php $dateOne = "2019-10-30"; $dateTwo = "2019-10-30"; echo "Date1 = $dateOne"; echo "
Date2 = $dateTwo"; if ($dateOne == $dateTwo) echo "
Both the dates are equal!"; else echo "Both the dates are different!"; ?>
输出
这将产生以下输出 -
Date1 = 2019-10-30 Date2 = 2019-10-30 Both the dates are equal!
示例
现在让我们看另一个示例 -
<?php $dateOne = "2019-11-08"; $dateTwo = "2018-08-10"; echo "Date1 = $dateOne"; echo "
Date2 = $dateTwo"; if ($dateOne < $dateTwo) echo "
DateTwo is the latest date!"; else echo "
DateOne is the latest date!"; ?>
输出
这将产生以下输出 -
Date1 = 2019-11-08 Date2 = 2018-08-10 DateOne is the latest date!
广告