duan205571 2015-09-15 21:26
浏览 32
已采纳

计算时间跨度的折扣小时数

I have a booking system where you can book a car for an arbitrary time and pay by the hour. Nighttime hours between 10pm and 8am are discounted. Does anyone have an elegant solution for calculating total minutes and discounted minutes for a booking, preferably in php?

My initial attempt includes calculating the full days, and finding the full price time and discount time for full days:

date_default_timezone_set('Europe/Oslo');
$discount_fraction=10/24;
$discount=0; 
$full=0; 
$from=strtotime("2015-09-16 12:00"); $to=strtotime("2015-09-20 14:00"); //example 
$total=$to-$from; // seconds
$full_days=floor($total/60/60/24)*24*60*60; // seconds

$discount+=$full_days*$discount_fraction; // discounted seconds 
$full+=(1-$discount_fraction)*$full_days; // full price seconds

Now I'm left with the reminder:

$reminder=fmod($total, 60*60*24);

However, this is where my troubles really start. I'm thinking there is some way to normalize the times so that I dont have to have more than a few if/else if's, but I cant make it work:

$from=date('H',$from)*60*60+date('i',$from)*60+date('s',$from); // seconds
$to=$reminder; // seconds 

$twentyfour=24*60*60; // useful values
$fourteen=14*60*60; 
$ten=10*60*60; 
$eight=8*60*60;
$twentyto=22*60*60;

This seems to work when $from-time < 8:

$d=0; 
$f=0; 
if($to<=($eight-$from)) $d=$reminder;
else if($to<=$twentyto-$from){
    $d=$eight-$from;
    $f=$to-($eight-$from); 
} 
else if($to<$eight+$twentyfour-$from){
    $f=$fourteen; 
    $d=$to-$fourteen; 
}
else if($to<$twentyto+$twentyfour-$from){
    $d=$ten;
    $f=$to-$ten;
}
$discount+=$d; 
$full+=$f; 

Anyone like to take a crack at it?

  • 写回答

1条回答 默认 最新

  • dongmo8943 2015-09-17 20:16
    关注

    I might as well post how I solved it eventually. Discount period from 2200 - 0800 at night. A little disappointed I had to go for looping over the booking instead of some more elegant approach which I'm sure exists, but this works and is good enough I guess.

    $interval=30; // allow thirty minutes interval, eg 12:30 - 14:00
    $int=$interval*60; // interval in seconds
    $twentyfour=24*60*60; // 24 hours in seconds
    $eight=8*60*60; // 8 hours in seconds
    $twentyto=22*60*60; // 22 hours in seconds
    
    // fraction of a 24-hour period thats discounted
    $discount_fraction=10/24; 
    $discount=0; // seconds at discount price
    $full=0; // seconds at full price
    $from=strtotime("2015-09-16 06:00");  //example input
    $to=strtotime("2015-09-20 04:00"); //example input
    $total=$to-$from; // Total number of seconds booked
    
    // full days booked, in seconds
    $full_days=floor($total/60/60/24)*24*60*60; 
    // discounted secs within the full days
    $discount+=$full_days*$discount_fraction; 
    // full price secs within the full days
    $full+=(1-$discount_fraction)*$full_days; 
    
    $reminder=fmod($total, 60*60*24); //remaining time less than 24 hours
    
    //start hour of remaining time
    $from=date('H',$from)*60*60+date('i',$from)*60+date('s',$from); 
    
    // looping from start-time to start+reminder,
    // using the order interval as increment, and 
    // starting as start plus 1/2 order-interval
    for($i=$from+$int/2; $i<$from+$reminder; $i+=$int){
        if(($i>0 && $i<$eight) || 
            ($i>$twentyto && $i<$twentyfour+$eight) ||
            ($i>$twentyfour+$twentyto)) {
                $discount+=$int; 
        }
        else{
            $full+=$int; 
        }
    }
    echo "Minutes at discount price ".($discount/60); 
    echo "Minutes at full price ".($full/60); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?