0

我有一个表格,它有两个日期取件和返回日期现在我想根据天数收费,但我陷入了循环。

我想在第 1、2、3、4 天收费,而不是 5、6、7、8 天不收费,而不是 9、10、11、12 天再次收费,而不是 13、14、15、16 天不收费,这样怎么样为这种模式编写代码,我尝试过代码,但没有尝试过

所以我可以选择添加费用。

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;  
4

3 回答 3

1

您不是在看四组,而是实际上是 8 组……您收取 8 的前半部分,而您不考虑后半部分,所以您想除以 8,看看余数是 4 还是较少的...

if ( $rental_days % 8 <= 4 && $rental_days % 8 != 0 )

无论我们有多少天,这都会起作用(例如:如果已经 300 天了怎么办)。您不想使用循环,因为这可能会永远持续下去。

于 2013-06-28T16:53:10.100 回答
1

我会改写你的问题:你正在寻找一个函数/算法,给定一个数字 N,将返回以下内容(在这里制作一个小表):

 N  return
 1     1
 2     2
 3     3
 4     4
 5     4
 6     4
 7     4
 8     4
 9     5
10     6

以下代码演示了如何执行此操作的简单方法 - 它仅在相同数量的付费天数 (4) 后跟相同数量的未付费天数 (4) 时有效:

<?php
for($n=1; $n<20; $n++)
  {
  $m = ($n-1)%4 + 1;               // returns 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4...
  $unpaid = intval(($n-1)/4)%2;    // returns 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,...
  $fourBlocks = intval(($n-1)/8);  // returns 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,...
  if ($unpaid == 1)
    {
    $daysCharged = $n - $m - 4 * $fourBlocks;
    }
  else
    {
    $daysCharged = $n - 4 * $fourBlocks;
    }
  echo $n . " days: " . $daysCharged . " days charged, ". ($n - $daysCharged) . " free\n";
  }
?>

这会产生以下输出:

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

注意 - 在更一般的情况下,您有 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 的值如何,都会为您提供正确的答案(因此您可以很容易地更改收费方案)。

于 2013-06-28T17:10:02.090 回答
0

这是您的解决方案,这正是您想要的。看看这个,希望它对你有用..

<?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
于 2013-06-28T17:45:30.640 回答