以24小时格式计算价格/ 30分钟

时间:2015-07-08 02:28:21

标签: algorithm

我正在开发一种预订系统理念的算法,并且在计算价格时陷入困境。

我正在使用一个简单的数组,将24小时分为48小时,每次30分钟。

$interval = {
0000, 0030, 0100, 0130, 0200, 0230, 0300, 0330, 0400, 0430, 0530, 0600,
0630, 0700, 0730, 0800, 0830, 0900, 0930, 1000, 1030, 1100, 1130, 1200,
1230, 1300, 1330, 1400, 1430, 1500, 1530, 1600, 1630, 1700, 1730, 1800,
1830, 1900, 1930, 2000, 2030, 2100, 2130, 2200, 2230, 2300, 2330 } 

到目前为止,我已经在我的程序员朋友的帮助下取得了成功,但我被困在一行,我需要找到所选持续时间内有多少个间隔(每个30分钟)。

$cost = 0;
$checkin_seconds = $checkin * 3600;

//MAIN PRICES ARE DECLARED HERE
for( $i=0;      $i<48;      $i++ ) {        
    if $checkin=$interval[i]   {
        $array_index_number=i;  }   //checkin time index_number from array


for( $start=$checkin;   $start<$checkout;   $start+30 ) { 

//PROBLEM: $start+30 will not work as computer doesn’t know we’re dealing with time, you will have to improvise here

$total_intervals++;  }  // total number of 30 mins intervals

$count=0;
for( $var=$array_index_number;  $var < $var + $total_intervals; $var++ ) { //check against those indexes
    if( $array[$var]>= 0000  &&  $array[$var]<=0400 ) {      //prices for 12AM to 4AM
                    $count++;  }
    $price = 2250 / 2;          //price for 60 minutes
$cost = $cost + ($price * $count);      }   // cost for intervals that lie in 12AM to 4AM

$count=0;
for( $var=$array_index_number;  $var < $var + $total_intervals; $var++ ) { //check against those indexes
    if( $array[$var]>= 0400  &&  $array[$var]<=0800 ) {      //prices for 4AM to 8AM
                    $count++;  }
    $price = 2500 / 2;          //price for 60 minutes
$cost = $cost + ($price * $count);      }   // cost for intervals that lie in 12AM to 4AM

$count=0;
for( $var=$array_index_number;  $var < $var + $total_intervals; $var++ ) { //check against those indexes
    if( $array[$var]>= 0800  &&  $array[$var]<=1600 ) {      //prices for 8AM to 4PM
                    $count++;  }
    $price = 1800 / 2;          //price for 60 minutes
$cost = $cost + ($price * $count);      }   // cost for intervals that lie in 8AM to 4PM

$count=0;
for( $var=$array_index_number;  $var < $var + $total_intervals; $var++ ) { //check against those indexes
    if( $array[$var]>= 1600  &&  $array[$var]<=2000 ) {      //prices for 4PM to 8PM
                    $count++;  }
    $price = 2000 / 2;          //price for 60 minutes
$cost = $cost + ($price * $count);      }   // cost for intervals that lie in 4PM to 8PM

$count=0;
for( $var=$array_index_number;  $var < $var + $total_intervals; $var++ ) { //check against those indexes
    if( $array[$var]>= 2000  &&  $array[$var]<=2400 ) {      //prices for 8PM to 12AM
                    $count++;  }
    $price = 2250 / 2;          //price for 60 minutes
$cost = $cost + ($price * $count);      }   // cost for intervals that lie in 8PM to 12AM

1 个答案:

答案 0 :(得分:0)

Modulo操作可以很好地解决时间问题。此解决方案假设您只使用整数,必要时应用地板函数。

计算时间间隔:

timeIntervals = 2*(timeNum / 100) + ((timeNum % 100) / 30)

所以你要找的是:

totalIntervals = (2*(checkout / 100) + ((checkout % 100) / 30)) - (2*(checkin / 100) + ((checkin % 100) / 30))