两次约会之间的PHP时间Y-m

时间:2017-01-13 14:36:42

标签: php date

我有这个代码,说明两个日期之间的天数,
但我需要它与Y-m而不是Y-m-d一起工作 怎么实现这个? 示例:我需要检查2016-102016-09

之间是否2016-12

我的代码:

$paymentDate = date('Y-m-d', strtotime($date));
$contractDateBegin = date('Y-m-d', strtotime($dfrom));
$contractDateEnd = date('Y-m-d', strtotime($dtom));

if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){

    echo "is between";

}else{

    echo "NO GO!";

}

2 个答案:

答案 0 :(得分:1)

使用Y-m是oK: Demo

$paymentDate=date('Y-m', strtotime($date));;
$contractDateBegin = date('Y-m', strtotime($dfrom));
$contractDateEnd = date('Y-m', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){

      echo "is between";
      }else{
      echo "NO GO!";  
      }

答案 1 :(得分:1)

提取月份和年份并进行比较。

$paymentDateY = date('Y', strtotime($date));
$paymentDateM = date('n', strtotime($date));

$contractDateBeginY = date('Y', strtotime($dfrom));
$contractDateBeginM = date('n', strtotime($dfrom));

$contractDateEndY = date('Y', strtotime($dtom));
$contractDateEndM = date('n', strtotime($dtom));


if ($paymentDateY >= $contractDateBeginY && $paymentDateY <= $contractDateEndY 
&& $paymentDateM >= $contractDateBeginM && $paymentDateM <= $contractDateEndM){
     echo "is between";
}
else
{
     echo "NO GO!";  
}