-4
我有一個預訂系統,您可以在此預訂任意時間的汽車並按小時付費。夜間10點至8點之間的時間可以打折。有沒有人有一個優雅的解決方案來計算預訂的總分鐘數和打折時間,最好在php中?計算時間範圍內的折扣時間數
我最初的嘗試包括計算整整兩天,找到了全天全價的時間和折扣時間:
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
現在我只剩下提醒:
$reminder=fmod($total, 60*60*24);
然而,這是我的麻煩真正開始的地方。我想有一些方法標準化的時代,使我不必有超過數的if/else若的,但我不能讓它工作:
$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;
這似乎是工作時$從 - 時間< 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;
任何人都喜歡對它進行破解?
我期待着代碼茲 – Kisaragi
向我們展示您迄今爲止解決您的問題的嘗試。 – D4V1D
如果客戶按小時付款,爲什麼要計算「分鐘」? –