Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting next increment with PHP

Tags:

php

increment

I have a value that increases by 1 every 5 views. The MySQL table looks like:

myvalue views increment
10      12    5

For instance, once the views reaches 15, myvalue will change to 11.

I acomplish this by doing:

$div = $myvalue / $increment;
if (ceil($div) == $div) {
    $myvalue = $myvalue + $increment;
}

What I am trying to do is do a quick calculation that would tell me how many more views to go before the next increment. In this example, I would like to return the number 3, since there are 3 more views until the next time myvalue hits an increment of 5, which is 15.

If myvalue was 10, views was 26 and increment was 25, I need to get the number 24, since there are 24 more views needed before we hit the next increment of 25, which is 50.

Any ideas how to do this?

like image 672
MultiDev Avatar asked Jun 18 '26 19:06

MultiDev


1 Answers

I think you can use this formula

$diff = $increment - ($views % $increment);

And also you original code could be written as

if(($views % $increment) === 0)
{
  $myvalue += 1; // you said myvalue will increase by one
}
like image 131
Josnidhin Avatar answered Jun 21 '26 07:06

Josnidhin