I'm looking to add .1 to the initial value of 0 every two weeks past a specified date.
Example:
Date = 05-26-2024; InitVal = 0; AddVal = 0.1;
The 26th was 4 weeks ago from 06-23-2024 which means 0 should now be 0.2.
I have had no luck finding something that would work here, or something I understand at least, on the internet so I come to you for help!
I've looked at several codes but they didn't quite fit what I was trying to do and I am incapable of actually modifying them to my needs. This is for a personal website and I just want to keep track of certain projects in a specific way.
PHP's time() and strtotime() both reports time in seconds. To know how many weeks there are between your specified date and now we can do:
$startDate = '2024-05-26 00:00:01';
$secondsInWeek = 60 * 60 * 24 * 7;
$weekDiff = (time() - strtotime($startDate)) / $secondsInWeek;
At this moment $weekDiff
would be around 4.125.
You want to add 0.1 to an initial value each two weeks:
$initial = 0.0;
$increase = 0.1;
$stepNo = intdiv(floor($weekDiff), 2);
$value = $initial + $stepNo * $increase;
Which will result in an $value
of 0.2. See: https://3v4l.org/gZgeY
Rounding down $weekDiff
with floor() results in 4.0. And dividing by 2 integer gives 2. Using this as a step number you can calculate your value. Note that intdiv() will always return the lower whole number, which is an integer. So intdiv(3.5,2)
is 1
.
If you happen to get a warning like:
Warning: Implicit conversion from float to int loses precision
You can use intval() for the argument.