两个日期之间的差异天数

时间:2016-08-08 06:40:07

标签: php codeigniter date datediff

$diff = strtotime( date("Y-m-d",strtotime($row->Released_Date)) ) - strtotime( date("Y-m-d",strtotime($CommitmentDate)));
$days = intval(round($diff/86400));

$s = $days > 1 ? 's' : '';

$status = $days > 0 ? '<i class="fa fa-exclamation" original-title="Day'.$s.' lapsed - '.$days.'<br><i>'.date("F d, Y",strtotime($CommitmentDate)).'</i>"></i>' :
          $days = 0 ? '<i class="fa fa-check" original-title="RELEASED ON TIME<br><i>'.date("F d, Y",strtotime($CommitmentDate)).'</i>"></i>' :
        '<i class="fa fa-star" original-title="RELEASED BEFORE COMMITMENT DATE<br><i>'.date("F d, Y",strtotime($CommitmentDate)).'<br>'.$days.'</i>"></i>';

有一个 $ days 返回,但它似乎没有输出完全,因为我没有看到检查标志...

我真的不知道什么是错的......

我用过

$days = intval(round($diff/86400));
$days = int(round($diff/86400);
我试过了     $ days = intval($ diff / 86400);

它返回的确切 ZERO ,但我不知道为什么输出比ZERO少

这是输出...结尾的数字是 $ days 的输出......

enter image description here enter image description here

请帮助......

2 个答案:

答案 0 :(得分:0)

您要分配$days=0,必须使用==运算符进行比较,

$days == 0 ? '<i class="fa fa-check" original-title="RELEASED ON TIME....

还有一件事你可以使用单一时间strtotime来获得差异,例如

// no need to convert it in date then again in strtotime
$diff = strtotime( $row->Released_Date ) - strtotime($CommitmentDate);

已更新,您需要将其他部分包含在()中,否则它不会按预期返回结果,

$status = $days > 0 ? '<i class="fa fa-exclamation" original-title="Day'.$s.' lapsed - '.$days.'<br><i>'.date("F d, Y",strtotime($CommitmentDate)).'</i>"></i>' :
      ($days == 0 ? '<i class="fa fa-check" original-title="RELEASED ON TIME<br><i>'.date("F d, Y",strtotime($CommitmentDate)).'</i>"></i>' :
  //--^ start enclosing here, with $days == 0 condition
    '<i class="fa fa-star" original-title="RELEASED BEFORE COMMITMENT DATE<br><i>'.
    date("F d, Y",strtotime($CommitmentDate)).'<br>'.$days.'</i>"></i>');
                                              // -- to here -----------^

答案 1 :(得分:0)

试试这个

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');
$daySec = $day * 86400;

来源:http://php.net/manual/en/datetime.diff.php