使用php创建数字系列
我有一个表格,有两个日期取货和返回日期,现在我想根据天数收费,但我陷入了循环 我想收费,如果第1,2,3,4天比5,6,7,8天不收费比9,10,11,12天再收费比13,14,15,16天不收费像这样如何为这种模式编写代码,我试过代码,但没有wokring 所以我可以另加费用使用php创建数字系列,php,series,Php,Series,我有一个表格,有两个日期取货和返回日期,现在我想根据天数收费,但我陷入了循环 我想收费,如果第1,2,3,4天比5,6,7,8天不收费比9,10,11,12天再收费比13,14,15,16天不收费像这样如何为这种模式编写代码,我试过代码,但没有wokring 所以我可以另加费用 if($rental_days%5==0 && $rental_days <10): $charge = ($rental_days-1)*$result['rent'];
if($rental_days%5==0 && $rental_days <10):
$charge = ($rental_days-1)*$result['rent'];
elseif($rental_days%6==0):
$charge = ($rental_days-2)*$result['rent'];
elseif($rental_days%7==0):
$charge = ($rental_days-3)*$result['rent'];
elseif($rental_days%5==0 && $rental_days >8):
$charge = ($rental_days-3)*$result['rent']+$result['rent'];
else:
$charge = ($rental_days-4)*$result['rent']+$result['rent'];
endif;
if($rental\u days%5==0&&$rental\u days 8):
$charge=($rental_days-3)*$result['rent']+$result['rent'];
其他:
$charge=($rental_days-4)*$result['rent']+$result['rent'];
endif;
您看到的不是四套,而是八套。。。8的前半部分你收费,后半部分你不收费,所以你想除以8,看看剩余的是4还是更少
if ( $rental_days % 8 <= 4 && $rental_days % 8 != 0 )
if($rental_days%8我将重新表述您的问题:您正在寻找一个函数/算法,给定一个数字N,它将返回以下内容(在此处制作一个小表格):
下面的代码演示了一种简单的方法,它只在相同的带薪天数(4)后面跟着相同的未付天数(4)时起作用:
注意-在更一般的情况下,如果您已收取N天费用,然后是M天未收取费用,您可以按如下方式更改代码:
<?php
$N = 4;
$M = 4;
echo $N . " days charged followed by " . $M . " days free:\n";
for($days=1; $days<20; $days++)
{
$numBlocks = intval(($days - 1) / ($N + $M)); // a "block" is a complete cycle of paid & unpaid days
$remainder = $days - ($N + $M) * $numBlocks; // the "remainder" contains an incomplete cycle
$unpaid = $remainder - $N;
if ($unpaid < 0) $unpaid = 0; // this now contains the number of unpaid days in the incomplete cycle
$daysCharged = $numBlocks * $N + $remainder - $unpaid; // easy to compute the number that must be charged
echo $days . " days: " . $daysCharged . " days charged, ". ($days - $daysCharged) . " free\n";
}
?>
这实际上更加可靠,无论M和N的值是多少,它都会给你正确的答案(因此你可以很容易地更改收费方案)。这是你的解决方案,这正是你想要的。检查一下,希望它对你有用
<?php
$charge = $counter = 40;
$switch = 0;
for($i=1;$i<=31;$i++)
{
if($switch == 0)
{
echo $i." => Charge = ".$charge."</br>";
$charge = $charge+$counter;
}
else if($switch == 1)
{
echo $i." => NoCharge = ".$charge."</br>";
}
if($i%4 == 0)
{
if($switch == 0)
{
$switch = 1;
if($i%8 == 0)
{
$charge = $charge+$counter;
}
}
else if($switch == 1)
{
$switch = 0;
if($i%8 == 0)
{
$charge = $charge+$counter;
}
}
}
}
?>
嗨,请帮帮我..我的收费是40,然后第1-40天,2-80天,3-120天,4-160天,5-200比7-200,8-200天,然后再次9-240,10-280,11-320,12-400比13-400,14-400,15-400天等等…如果你在第5天收费200,那么第5天不是免费的?…你是在改变问题还是说目前提出的解决方案是无效的“不为你工作?我不想从一家写着“第五天不收费”的公司租一辆车,然后给我一张第五天的账单。我承认最初的问题和随后对其中一个问题的评论相互矛盾。。。
1 days: 1 days charged, 0 free
2 days: 2 days charged, 0 free
3 days: 3 days charged, 0 free
4 days: 4 days charged, 0 free
5 days: 4 days charged, 1 free
6 days: 4 days charged, 2 free
7 days: 4 days charged, 3 free
8 days: 4 days charged, 4 free
9 days: 5 days charged, 4 free
10 days: 6 days charged, 4 free
11 days: 7 days charged, 4 free
12 days: 8 days charged, 4 free
13 days: 8 days charged, 5 free
14 days: 8 days charged, 6 free
15 days: 8 days charged, 7 free
16 days: 8 days charged, 8 free
17 days: 9 days charged, 8 free
18 days: 10 days charged, 8 free
19 days: 11 days charged, 8 free
<?php
$N = 4;
$M = 4;
echo $N . " days charged followed by " . $M . " days free:\n";
for($days=1; $days<20; $days++)
{
$numBlocks = intval(($days - 1) / ($N + $M)); // a "block" is a complete cycle of paid & unpaid days
$remainder = $days - ($N + $M) * $numBlocks; // the "remainder" contains an incomplete cycle
$unpaid = $remainder - $N;
if ($unpaid < 0) $unpaid = 0; // this now contains the number of unpaid days in the incomplete cycle
$daysCharged = $numBlocks * $N + $remainder - $unpaid; // easy to compute the number that must be charged
echo $days . " days: " . $daysCharged . " days charged, ". ($days - $daysCharged) . " free\n";
}
?>
<?php
$charge = $counter = 40;
$switch = 0;
for($i=1;$i<=31;$i++)
{
if($switch == 0)
{
echo $i." => Charge = ".$charge."</br>";
$charge = $charge+$counter;
}
else if($switch == 1)
{
echo $i." => NoCharge = ".$charge."</br>";
}
if($i%4 == 0)
{
if($switch == 0)
{
$switch = 1;
if($i%8 == 0)
{
$charge = $charge+$counter;
}
}
else if($switch == 1)
{
$switch = 0;
if($i%8 == 0)
{
$charge = $charge+$counter;
}
}
}
}
?>
1 => Charge = 40
2 => Charge = 80
3 => Charge = 120
4 => Charge = 160
5 => NoCharge = 200
6 => NoCharge = 200
7 => NoCharge = 200
8 => NoCharge = 200
9 => Charge = 240
10 => Charge = 280
11 => Charge = 320
12 => Charge = 360
13 => NoCharge = 400
14 => NoCharge = 400
15 => NoCharge = 400
16 => NoCharge = 400
17 => Charge = 440
18 => Charge = 480
19 => Charge = 520
20 => Charge = 560
21 => NoCharge = 600
22 => NoCharge = 600
23 => NoCharge = 600
24 => NoCharge = 600
25 => Charge = 640
26 => Charge = 680
27 => Charge = 720
28 => Charge = 760
29 => NoCharge = 800
30 => NoCharge = 800
31 => NoCharge = 800