首页 > 解决方案 > 如何理解 php 中的一个日期是否小于另一个减去一天?

问题描述

如何理解 php 中的一个日期是否小于另一个减去一天?我的意思是,例如,如果将日期设置为“2018/07/03 ;我如何理解给定日期是否小于“ 2018/07/02

date1 : year1/month1/day1 date2: year2/month2/day2

<?php
if ($year1 >= $year2) {
        if ($month1 >= $month2) {
                  if (($day1 - 1) > $day2) {
                      echo 'you could do something..';
                   }
          }
}
?>

如果例如 $year2 = 2017 和 $month2 = 11 .. 任何人都可以帮助我,上面的代码将失败?多谢..

标签: phpdate

解决方案


在这里,这应该工作。

$date_to_check = new DateTime($yesterday);
$today = new DateTime();
$time_diff = $today->diff($date_to_check)->d;

if($time_diff > 1) {
echo "This is greater than one day.";
}else{
echo "This is not greater than one day.";

推荐阅读