PHP - 每天按周按月按固定价格计算

PHP - Calculate as per daily weekly and monthly basis based on fixed prices

我在计算租赁产品的每日、每周和每月费率时遇到问题。

每天 310 次/每周 725 次/每月 1,660 次

当谈到逻辑时:

Day 1 + day 2 = 620Day 1 + day 2 + day 3 = 930 大于 725(每周).所以这里应该应用周价格直到达到 7 天。

现在,
第8天=第7天+310=1035(不>1660月租)
第9天=第8天+310=1345(不>1660月租)
Day 10 = Day 9 + 310 =1655 (Not > 1660 MONTHLY RENT) 但是,它大于 2 周价格且天数小于14.
所以在这里它应该应用 725+725=1450 到 14 天。
.....
.....
第 14 天 = 1450

现在,
第 15 天 = 第 14 天 +310 = 1760 ( > 1660 )
在这里,它应该应用 1660 直到月底。
.....
.....
第 30/31 天 = 1660

我尝试了很多构建但没有成功。谁能帮帮我。

更新代码:

<?php

$perDayAmt=310;
$perWeekAmt=725;
$perMonthAmt=1660;

$finalAmt=0;
$start_date=new DateTime(date('Y-m-d'));//, strtotime("+12 days")
$end_date=new DateTime(date('Y-m-d', strtotime("+14 days")));

$differenceInDays = $end_date->diff($start_date)->format("%a");
if($differenceInDays >0){

if($differenceInDays <= 7){
    if(($differenceInDays*$perDayAmt) <= $perWeekAmt){// 5 days amount <= $perWeekAmt amount
        $finalAmt = $differenceInDays*$perDayAmt; //no. of days * $perDayAmt amount
    }else{
        $finalAmt =$perWeekAmt; // $perWeekAmt amount
    }
}else{
    /*if( ($differenceInDays%7) == 0 && ($perWeekAmt*($differenceInDays/7)) < $perMonthAmt){
        //14 < 30 && 14 % 7==0 && $perWeekAmt amount * [2 or 3 or 4 or 5] < $perMonthAmt amount
        $finalAmt = $perWeekAmt*($differenceInDays/7); //$perWeekAmt amount * [2 or 3 or 4 or 5]
    }else if(($differenceInDays%7) != 0 && ($perWeekAmt*($differenceInDays/7)) < $perMonthAmt){
        //10 < 30 &&  10 % 7!=0 && 10 days amount > $perWeekAmt amount
        $finalAmt =$perWeekAmt; // $perWeekAmt amount
    }*/
    if( ($differenceInDays%7) == 0) {
        if(($perWeekAmt*($differenceInDays/7)) > $perMonthAmt){
            if( ($differenceInDays%30) == 0) {
                $finalAmt = $perMonthAmt*($differenceInDays/30);
            }else{

            }

        }else if(($perWeekAmt*($differenceInDays/7)) < $perMonthAmt){
            $finalAmt = $perWeekAmt*($differenceInDays/7);
        }
    }
}

echo $finalAmt;exit;    
}

这是参考资料link您可以将产品添加到购物车:

https://www.zieglerrental.com/equipment/skid-steer-compact-loaders/cat-compact-track-loaders/cat-239d-compact-track-loader/

与其编写 ifswitch-case 语句块,使用带有三个变量的 min() 将简洁地执行此任务。请参阅下面我的演示代码段的详细输出,了解每次 $diff 出现时比较的三个值。

代码:(Demo)

$pricing=['day'=>310,'week'=>725,'month'=>1660];
for($x=0; $x<33; ++$x){
    echo "$x : ";
    $finalAmt=0;
    $start_date=new DateTime('today');
    $end_date=new DateTime(date('Y-m-d',strtotime("+$x days")));

    $diff=$end_date->diff($start_date);

    if($diff->y){$finalAmt+=$diff->y*12*$pricing['month'];}

    if($diff->m){$finalAmt+=$diff->m*$pricing['month'];}

    if($diff->d){
        $weeks=floor($diff->d/7);
        $days=$diff->d%7;
        $raw=$weeks*$pricing['week']+$days*$pricing['day'];
        $weekup=$pricing['week']*($weeks+1);
        $finalAmt+=min($raw,$weekup,$pricing['month']);  // <-- here is the magic; min() on three variables
    }
    echo "$$finalAmt";
    if($diff->d){echo " is min of: $raw & $weekup & {$pricing['month']}";}
    echo "\n";
}

输出:

0 : [=11=]
1 : 0 is min of: 310 & 725 & 1660
2 : 0 is min of: 620 & 725 & 1660
3 : 5 is min of: 930 & 725 & 1660
4 : 5 is min of: 1240 & 725 & 1660
5 : 5 is min of: 1550 & 725 & 1660
6 : 5 is min of: 1860 & 725 & 1660
7 : 5 is min of: 725 & 1450 & 1660
8 : 35 is min of: 1035 & 1450 & 1660
9 : 45 is min of: 1345 & 1450 & 1660
10 : 50 is min of: 1655 & 1450 & 1660
11 : 50 is min of: 1965 & 1450 & 1660
12 : 50 is min of: 2275 & 1450 & 1660
13 : 50 is min of: 2585 & 1450 & 1660
14 : 50 is min of: 1450 & 2175 & 1660
15 : 60 is min of: 1760 & 2175 & 1660
16 : 60 is min of: 2070 & 2175 & 1660
17 : 60 is min of: 2380 & 2175 & 1660
18 : 60 is min of: 2690 & 2175 & 1660
19 : 60 is min of: 3000 & 2175 & 1660
20 : 60 is min of: 3310 & 2175 & 1660
21 : 60 is min of: 2175 & 2900 & 1660
22 : 60 is min of: 2485 & 2900 & 1660
23 : 60 is min of: 2795 & 2900 & 1660
24 : 60 is min of: 3105 & 2900 & 1660
25 : 60 is min of: 3415 & 2900 & 1660
26 : 60 is min of: 3725 & 2900 & 1660
27 : 60 is min of: 4035 & 2900 & 1660
28 : 60 is min of: 2900 & 3625 & 1660
29 : 60 is min of: 3210 & 3625 & 1660
30 : 60 is min of: 3520 & 3625 & 1660
31 : 60
32 : 70 is min of: 310 & 725 & 1660