0

Basically, there is a "type" in a database that has "Probation" in it. There is also a time stamp of "2017-01-15 22:18:44" for example in RealDate. No matter how long ago the date is, the difference still returns true like it's within three days. Any suggestions?

$doR = $totalArr[$i]['RealDate'];
$start = strtotime($doR);
$today = time();
$diff = ($start-$today)/(60 * 60 * 24);
if($diff < 5){
    $parole = "<span style=\"color:red\"> - ON PAROLE</span>";
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Austin M-S
  • 49
  • 5
  • strtotime is not what you seem to think it is .. `/DAY` and DAY is what? –  Feb 03 '17 at 02:52
  • I thought DAY will tell you how many days. So (time/day)=days... I guess not? – Austin M-S Feb 03 '17 at 02:55
  • php has no DAY function, this code should produce erros –  Feb 03 '17 at 02:57
  • http://ideone.com/vSd9wb PHP Notice: Use of undefined constant DAY - assumed 'DAY' in /home/pkXCIz/prog.php on line 3 PHP Warning: Division by zero in /home/pkXCIz/prog.php on line 3 –  Feb 03 '17 at 02:58
  • Anyone have a proper route I should take? – Austin M-S Feb 03 '17 at 02:59
  • supply the number of seconds in a day, or better still use the date\time class:http://php.net/manual/en/class.datetime.php –  Feb 03 '17 at 03:00
  • and please develop will full error checking and display ON –  Feb 03 '17 at 03:01
  • I've looked at the datetime class, and I seem to get overwhelmed. Can you provide me with the code I need? @nogad – Austin M-S Feb 03 '17 at 03:06
  • currently only have the time for the quick and dirty: `$diff = ($start-$today)/(60 * 60 * 24); ` oh then its `if($diff < 3)`... –  Feb 03 '17 at 03:08
  • See above for updated code. It still returned $parole, and it shouldn't. – Austin M-S Feb 03 '17 at 03:26
  • http://ideone.com/kbzNGZ works for me, you need to start echoing every variable. –  Feb 03 '17 at 03:56

1 Answers1

1

The problem is you're subtracting $start-$today when you should be doing it the other way around $today-$start

$start-$today will always be negative since the time now will always be a larger number than a past time in UNIX timestamp format. Since it's always negative, that's why your condition of $diff < 5 is always true.

$doR = $totalArr[$i]['RealDate'];
$start = strtotime($doR);
$today = time();

// change it like this:
$diff = ($today-$start)/(60 * 60 * 24);

if($diff < 5){
    $parole = "<span style=\"color:red\"> - ON PAROLE</span>";
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167