在递增数字中找到第一个1/2然后第一个整体

时间:2011-04-22 14:42:51

标签: php math increment

我正在使用php来运行日期列表。每个工作日的日期被分配1的百分比,然后按百分比递增。

我需要能够将第一个增量标记为等于或大于.5然后第一个增量等于或大于1,然后等于或大于.5然后对每个整数都相同。

我遇到问题的问题并不是真正找到要标记的数字,而是忽略结果直到它再次出现。

我不能比过去更圆,以至于从长远来看会以不同的方式伤害任何一方。

这是我所拥有的一小部分内容的输出,希望它更有意义。

2011-03-06 Weekend 
2011-03-07 Earned: 0.096154 Accrued: 0.096154
2011-03-08 Earned: 0.096154 Accrued: 0.192308
2011-03-09 Earned: 0.096154 Accrued: 0.288462
2011-03-10 Earned: 0.096154 Accrued: 0.384615
2011-03-11 Earned: 0.096154 Accrued: 0.480769
2011-03-12 Weekend 
2011-03-13 Weekend 
2011-03-14 Earned: 0.096154 Accrued: 0.576923 <- should be marked
2011-03-15 Earned: 0.096154 Accrued: 0.673077
2011-03-16 Earned: 0.096154 Accrued: 0.769231
2011-03-17 Earned: 0.096154 Accrued: 0.865385
2011-03-18 Earned: 0.096154 Accrued: 0.961538
2011-03-19 Weekend 
2011-03-20 Weekend 
2011-03-21 Earned: 0.096154 Accrued: 1.057692 <- should be marked
2011-03-22 Earned: 0.096154 Accrued: 1.153846
2011-03-23 Earned: 0.096154 Accrued: 1.25
2011-03-24 Earned: 0.096154 Accrued: 1.346154
2011-03-25 Earned: 0.096154 Accrued: 1.442308
2011-03-26 Weekend 
2011-03-27 Weekend 
2011-03-28 Earned: 0.096154 Accrued: 1.538462 <- should be marked 
2011-03-29 Earned: 0.096154 Accrued: 1.634615
2011-03-30 Earned: 0.096154 Accrued: 1.730769
2011-03-31 Earned: 0.096154 Accrued: 1.826923
2011-04-01 Earned: 0.096154 Accrued: 1.923077
2011-04-02 Weekend 
2011-04-03 Weekend 
2011-04-04 Earned: 0.096154 Accrued: 2.019231 <- should be marked
2011-04-05 Earned: 0.096154 Accrued: 2.115385
2011-04-06 Earned: 0.096154 Accrued: 2.211538
2011-04-07 Earned: 0.096154 Accrued: 2.307692
2011-04-08 Earned: 0.096154 Accrued: 2.403846
2011-04-09 Weekend 
2011-04-10 Weekend 
2011-04-11 Earned: 0.096154 Accrued: 2.5  <- should be marked
2011-04-12 Earned: 0.096154 Accrued: 2.596154
2011-04-13 Earned: 0.096154 Accrued: 2.69230

添加请求的代码。这里有很多输出,所以我可以看到它正在做什么。一旦我知道它正在做我需要做的事情,它就会被删除。

关于$ x的注意事项这里是静态的,但是会是动态的,所以我必须担心第一个小数点可能并不总是有一个5来告诉我它大概是1/2

$startDate = '2011-01-01';
$x= 25/260; 
$y = 0;

while (strtotime($startDate) <= strtotime($today)) {

echo $startDate;
    if(is_weekday($startDate)) {
    $y = $x+$y;
    print ' Earned: '.round($x,6).' Accrued: '.round($y,6).'<br />';
    } else {
    print ' Weekend <br />';
    } 
$startDate = date ('Y-m-d', strtotime('+ 1 day', strtotime($startDate)));
}

2 个答案:

答案 0 :(得分:1)

counter设置为1.继续,直到找到大于或等于counter * 0.5的值。标记它,将counter递增1并重复。

答案 1 :(得分:0)

$count = 1;
while(your loop for displaying said content, foreach() maybe)
{

   echo "{$timestamp} Earned: {$earned} Accrued: {$accrued}";
   if($accrued > ($count * 0.5)) { echo "<- should be marked"; $count++;}
   echo "<br/>";

}