strtotime 在 -60 天内无法正常工作

strtotime is not correctly working for -60 days

我正在尝试比较两个日期,但出于某种原因在我看来使用 -60 天对 strtotime 不起作用。

$date =  date('Y-m-d H:i:s', strtotime("-60 days")); // gives me string) 2016-09-25 09:27:26
$today_dt = strtotime($date);
$expire_dt = strtotime($result['insertedInvoice']); // insertedInvoice shows 2016-11-24 08:56:48

if( $today_dt > $expire_dt){
 dothis();
}else{
  dothat();
}

问题是,在这个函数中,它应该选择 dothis();,而不是选择 dothat(); 因为 $today_dt 已经晚了 60 天,所以它不能大于$expire_dt。

尝试关注,

$now = date('Y-m-d H:i:s');

$date =  date('Y-m-d H:i:s', strtotime('-60 days', strtotime($now)));
$today_dt = strtotime($date);
$expire_dt = strtotime($result['insertedInvoice']); // insertedInvoice shows 2016-11-24 08:56:48

if( $today_dt > $expire_dt){
 dothis();
}else{
  dothat();
}

** 我按照你的代码写了这段代码,请检查 if 条件。如果需要改变 if 条件如下 (我对你的最后一句话有点困惑)

if( $today_dt < $expire_dt){
   dothis();
} else {
   dothat();
}

您可以改用 DateTime:

$date = new DateTime( '-60 days' );
echo $date->format( 'Y-m-d H:i:s' );
$today = date('Y-m-d H:i:s'); //Today
echo $date = date('Y-m-d H:i:s', strtotime($today . ' - 60 days'));

Hope this will helps you.