PHP - 根据固定价格按每日和每月计算

时间:2017-11-30 10:15:58

标签: php datediff price

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

每日310件/每周725件/每月1,660件

说到逻辑:

第1天+第2天= 620 第1天+第2天+第3天= 930 <其大于725(每周)。所以这周的价格应该适用到7天。

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

现在,
第15天=第14天+310 = 1760 (&gt; 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;    
}

以下是您可以将产品添加到购物车的参考链接:

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

1 个答案:

答案 0 :(得分:0)

使用带有三个变量的if而不是编写switch-casemin()语句块,而不是简单地执行此任务。请参阅下面我的演示代码段中的详细输出,了解每个$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 : $0
1 : $310 is min of: 310 & 725 & 1660
2 : $620 is min of: 620 & 725 & 1660
3 : $725 is min of: 930 & 725 & 1660
4 : $725 is min of: 1240 & 725 & 1660
5 : $725 is min of: 1550 & 725 & 1660
6 : $725 is min of: 1860 & 725 & 1660
7 : $725 is min of: 725 & 1450 & 1660
8 : $1035 is min of: 1035 & 1450 & 1660
9 : $1345 is min of: 1345 & 1450 & 1660
10 : $1450 is min of: 1655 & 1450 & 1660
11 : $1450 is min of: 1965 & 1450 & 1660
12 : $1450 is min of: 2275 & 1450 & 1660
13 : $1450 is min of: 2585 & 1450 & 1660
14 : $1450 is min of: 1450 & 2175 & 1660
15 : $1660 is min of: 1760 & 2175 & 1660
16 : $1660 is min of: 2070 & 2175 & 1660
17 : $1660 is min of: 2380 & 2175 & 1660
18 : $1660 is min of: 2690 & 2175 & 1660
19 : $1660 is min of: 3000 & 2175 & 1660
20 : $1660 is min of: 3310 & 2175 & 1660
21 : $1660 is min of: 2175 & 2900 & 1660
22 : $1660 is min of: 2485 & 2900 & 1660
23 : $1660 is min of: 2795 & 2900 & 1660
24 : $1660 is min of: 3105 & 2900 & 1660
25 : $1660 is min of: 3415 & 2900 & 1660
26 : $1660 is min of: 3725 & 2900 & 1660
27 : $1660 is min of: 4035 & 2900 & 1660
28 : $1660 is min of: 2900 & 3625 & 1660
29 : $1660 is min of: 3210 & 3625 & 1660
30 : $1660 is min of: 3520 & 3625 & 1660
31 : $1660
32 : $1970 is min of: 310 & 725 & 1660