开发者

PHP formula to assign days incrementally increasing point values, separated by value ranges of 30

I am using a PHP variable that calculates the number of days between today and when an entry was created:

DATEDIFF(NOW(), l.created)

How could I write a PHP equation where the first 30 days of this value are multiplied by 1 point each, then days 30-60 by 2 points each, then 60-90 by 3 points each, etc. up until the total number of days since creation, and then all points summed.

So if something has only been created for 15 days, it is then given 15 points. But if something has been created for 45 days, it is given 60 points (3开发者_运维知识库0 * 1 plus 15 * 2)?


Algorithm:

$d = <number of days>;

$m = intval($d / 30);  // completed months
$r = intval($d % 30);  // remaining days beyond that

$score = ($m * ($m + 1) / 2) * 30 + ($m + 1) * $r;

//       ^^^^^^^^^^^^^^^^^^^        ^^^^^^^^
//       accumulated levels        final lvl.

Example: For $d = 45, we have $m = 1 and $r = 15, yielding 30 + 2*15 = 60.


Here's a free-standing formulation of this in SQL, but you have to hook it up to your function yourself with a subquery or a join or something:

SELECT TRUNCATE((TRUNCATE(d/30, 0) * (TRUNCATE(d/30, 0) + 1) / 2) * 30 + (TRUNCATE(d/30, 0) + 1) * (d % 30), 0) AS score
FROM (SELECT 45 AS d) AS t;   


for ($i = 0; $i < $number; $i++) $points += ceil($i / 30);


<?
$day_num = 90;
$points = 0;
for($i=1;i<=$day_num;$i++)
{
    $points += ceil($i/30);
}
echo $points;
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜