用strtotime计算两个日期之间的差异

时间:2014-10-17 16:41:18

标签: php mysql

我想回想一下变量和现在之间剩下多少天。这是代码

$now = date('Y-m-d'); 
$daysleft=  strtotime($starttheproject -$now);

echo "Now =".$now. "<br> Starttheproject =".$starttheproject. "<br> Daysleft =".$daysleft;

回音结果:

  • Row = 2014-10-17
  • Starttheproject = 2014-10-22
  • Daysleft =

问题: 如何计算天数,以便结果将是&#39; 5&#39; 我一直在玩这样的代码,没有运气:

$daysleft=  date('Y-m-d', strtotime($starttheproject -$now));

1 个答案:

答案 0 :(得分:0)

试试这个:

$date = new DateTime('2014-03-10');
$finish = new DateTime();
$difference = $date->diff($finish);
$difference =$difference->format('%R%a');
if ($difference > 10) {  //if the post is over 10 days old
  echo 'Renew now';
}else{
  echo 'Renew in '.$difference.' days</font>';
}