1

So I want to get our current date today (m-d-Y) then add 30 days. I would put this in a mySQL database. Then each day it will say how many days left when it is close to 14 days.

$today = strtotime(date('Y-m-d H:i:s'));
$expireDay = date('m-d-Y', strtotime("+30 days"));
$timeToEnd = $expireDay - $today;
if ($timeToEnd <= strtotime("$expireDay - 14 days")// this is not correct syntax
{
echo "less than 14 days";
echo $timeToEnd('d')//here is where I am having problems with my syntax
} else {
echo "more than 14 days";
}
DDJ
  • 807
  • 5
  • 13
  • 31
  • `$expireDay` is a string, not an integer. If you use `strtotime` you can run math on it to get the data. Also `strtotime(date('Y-m-d H:i:s'))` can just be `time()`. – chris85 Mar 27 '16 at 20:55
  • This question is being asked almost every second day. http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php – Axalix Mar 27 '16 at 21:13

1 Answers1

2

I assume $today is just for testing if that is the case this code should work. Your code was trying to subtract a string which you can't do. date('m-d-Y', strtotime("+30 days")); is a date string 03-27-2016, to subtract you need an integer which strtotime will give.

$today = time();
$expireDay = strtotime("+30 days");
$daysleft = (($expireDay - $today) / 86400);
if ($daysleft <= 14 ) {
     echo "less than 14 days";
     echo $daysleft;
} else {
     echo "more than 14 days";
}
chris85
  • 23,846
  • 7
  • 34
  • 51