0

I want to echo how much days are left between a variable and NOW. This is the code

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

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

Result of echo:

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

Question: How can calculate the number of days, so that the result will be '5' I've been playing with code like this, with no luck:

$daysleft=  date('Y-m-d', strtotime($starttheproject -$now));
akkie
  • 49
  • 3
  • check for DateTime::Diff – Abhik Chakraborty Oct 17 '14 at 16:42
  • when I use this: $daysleft = abs(strtotime($starttheproject) - strtotime($now)); then the result is 3891600 @AbhikChakraborty I tried: $daysleft = $starttheproject->diff($now); – akkie Oct 17 '14 at 16:43
  • I've checked that post already before posting. The solution was not working in my case. Result was "0 years, 0 months, 0 days" – akkie Oct 17 '14 at 16:52
  • [***Looks just fine to me, I get 5 days as expected.***](https://eval.in/207243) – Prix Oct 17 '14 at 16:53
  • $starttheproject is a String???, strtotime($starttheproject) -$now instead strtotime($starttheproject -$now) – esdebon Oct 17 '14 at 17:01

1 Answers1

0

Try with this:

$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>';
}
esdebon
  • 2,460
  • 5
  • 26
  • 33