Need help with a little math please

once again, more new ground for me. I probably need some obscure code to get this right. any thoughts please?

have a difference of two timestamps… diff in seconds, ex. 1209600.
I divide by whatever it is to convert to full days, say 14 days.
I also have a few “HIT” frequencies, say one’s every 7 days.

NOW what i’m trying to do is this:

look at a particular diff/frequency combo, and figure if it divides evenly into a whole number without any remainder. 14/7 = 2 (a HIT), 1358/14 = 97 (a HIT), etc…

code would look like this:
if (diff = 0) or (diff/frequency = a whole number){a HIT}

that’s the ideal, but… now for extra credit…
for some reason my diffs are not exact, but real close - ALWAYS within 1%.

so what will do is this:
if (diff = 0) or (diff/frequency = a whole number +\- 1%){a HIT}

figure solution will run negative values too.

so there you have it. any and all comments are greatly appreciated! thank you!!

Part 1

Does y divide x evenly?

Two solutions:

  1. (x % y == 0) will be true. % is the modulus operator and gives the remainder of integer division (like how you did long division on paper in elementary school). The remainder is 0 if two numbers divide evenly.

  2. ((x / y) == round(x / y)) will be true. If the result of division had a fractional part, then it would not be equal to the rounded version of that result, which by definition never has a fractional part.

Part 2

This depends on what you want to be within +/- 1% of. The divisor or the quotient?

One possibility:

((x % y) / y) < 0.01


((x / y) - floor(x / y)) < 0.01

thanks a lot Dan, very helpful, all of it… you rock !

if ($difference % $every == 0){
echo “<br>” . “A HIT”;}

works fine.

if ((($difference / $every) - floor($difference / $every)) < 0.01){
echo “<br>” . “A HIT”;}

works fine when $difference is < zero ONLY.

so I added this: seems to be working fine.

if ((ceil($difference / $every) - ($difference / $every)) < 0.01){
echo “<br>” . “A HIT”;}

thanks again for the help!