2013-01-09 404 views
0

我已經創建了下面的類,即計算時間範圍之前的時間,時間範圍內的時間範圍以及時間範圍之後的時間範圍。時間計算類問題

更具體一點,我創建了一個出租車預訂系統。出租車一般收費爲一日遊,夜間收費爲雙倍收費。

該網站的所有者,有能力設置時間的夜晚率開始和結束。作爲例子,讓我們說,正常出租車夜間利率在00:00開始和05:00結束,併爲微型客車出租車夜間費,開始在23:00結束06:00

在客戶下訂單時,谷歌地圖計算出行時間,所以我們假設這次旅行時長爲兩小時。

基於這種方案,在出租車最終用戶必須充電與正常率1小時1小時雙率的情況下。在微型客車的情況下最終用戶必須充電2小時雙率,而微型客車夜間率開始於23:00

與我的班級一樣,在當前狀態下,第一個例子工作正常,但第二個例子是錯誤的,我找不到原因。

<?php 
class tm 
{ 
    private $start_hour  = 0; // The hour that the trip starts 
    private $start_minute = 0; // The minute that the trip starts 
    private $from_hour  = 0; // The hour that night rates start 
    private $from_minute = 0; // The minute that night rates start 
    private $to_hour  = 0; // The hour that night rates ends 
    private $to_minute  = 0; // The minute that night rates ands 
    private $duration  = 0; // Total trip duration 
    private $night_duration = 0; // The overall night trip time in minutes 
    private $day_duration = 0; // The overall day trip time in minutes 
    private $totalHours  = 0; // The total duration hours 
    private $totalMinutes = 0; // The total duration minutes 
    private $extraMinutes = 0; // Extra minutes to calculate 

    /** 
    * Construct duration calculator class 
    * 
    * @param $start_date  Timestamp | The trip start date/time as a timestamp 
    * @param $duration  Seconds  | The duration time in seconds 
    * @param $night_start Timestamp | The date/time that night rates start as a timestamp 
    * @param $night_end  Time  | The date/time that night rates end as a timestamp 
    */ 
    public function __construct($start_date = '', $duration = 1, $night_start = '', $night_end = '') 
    { 
     $this->start_hour = date('H', $start_date); 
     $this->start_minute = date('i', $start_date); 
     $this->duration  = ($duration/60);    // Convert seconds to minutes 
     $this->from_hour = date('H', $night_start); 
     $this->from_minute = date('i', $night_start); 
     $this->to_hour  = date('H', $night_end); 
     $this->to_minute = date('i', $night_end); 

     $this->calculate(); 
    } 

    private function calculate() 
    { 
     $this->getHoursMinutes(); 
     $current_hour = $this->start_hour % 24; 
     $is_first_loop = true; 

     for($minute = 0; $minute < $this->duration; $minute++) 
     { 
      $current_minute  = ($this->start_minute + $minute) % 60; 

      if($current_minute == 0 && $is_first_loop == false) 
      { 
       $current_hour = ($current_hour + 1) % 24; 
      } 
      else if($current_minute == 59) 
      { 
       $is_first_loop = false; 
      } 

      if(($current_hour >= $this->from_hour && $current_hour < $this->to_hour)) 
      { 
       $this->night_duration++; 
      } 
      else 
      { 
       $this->day_duration++; 
      } 
     } 
    } 

    private function getHoursMinutes() 
    { 
     $this->totalHours = round($this->duration/60/60, 0); 
     $this->totalMinutes = round($this->duration/60, 0); 
     $this->extraMinutes = round(($this->duration/60) % 60, 0); 
    } 

    public function getDayDuration($inSeconds = true) 
    { 
     if($inSeconds == true) 
     { 
      return $this->day_duration * 60; 
     } 
     else 
     { 
      return $this->day_duration; 
     } 
    } 

    public function getNightDuration($inSeconds = true) 
    { 
     if($inSeconds == true) 
     { 
      return $this->night_duration * 60; 
     } 
     else 
     { 
      return $this->night_duration; 
     } 
    } 
} 
?> 

基於以上假設我有以下代碼示例:

<?php 
    $trip_start_timestamp = strtotime('01/09/2013 23:00'); 
    $duration    = strtotime('01/10/2013 01:00') - $trip_start_timestamp; 
    $night_start    = strtotime('00:00:00');   // This is the time for taxi start night rate 
    $night_end    = strtotime('05:00:00');   // This is the time for taxi end night rate 

    $taxi  = new tm($trip_start_timestamp, $duration, $night_start, $night_end); 

    echo "TAXI NIGHT DURATION<br />"; 
    echo "Day : " . $taxi->getDayDuration() . '<br />'; 
    echo "Night : " . $taxi->getNightDuration() . '<br />'; 

    $trip_start_timestamp = strtotime('01/09/2013 23:00'); 
    $duration    = strtotime('01/10/2013 01:00') - $trip_start_timestamp; 
    $night_start    = strtotime('23:00:00');   // This is the time for taxi start night rate 
    $night_end    = strtotime('06:00:00');   // This is the time for taxi end night rate 

    $miniBus = new tm($trip_start_timestamp, $duration, $night_start, $night_end); 

    echo "<br />MINI BUS NIGHT DURATION<br />";   
    echo "Day : " . $miniBus->getDayDuration() . '<br />'; 
    echo "Night : " . $miniBus->getNightDuration() . '<br />'; 
?> 

完上面的代碼的結果如下:

TAXI NIGHT DURATION 
Day : 3600 
Night : 3600 



MINI BUS NIGHT DURATION 
Day : 3600 
Night : 3600 

正如你所看到的上面的結果是錯誤的,因爲小巴夜間費率開始於23:00,所以結果應該是日期:0晚上:7200

最後請注意,我在線上施加幾處修改:

if(($current_hour >= $this->from_hour && $current_hour < $this->to_hour)) 

因爲我認爲,問題是這條線。不幸的是我沒有找到任何解決方案。

+1

1.感覺就像你在錯誤地做這件事2.你可以擴展類(爲miniBus製作專門的版本)3。你的班級只計算日/夜費率的小時數? – Khez

+0

1.你有更好的解決方案嗎? 2.汽車是動態生成的,所以不能預測類別。 3.班級計算晝夜旅行量。在上面的例子中,這次旅行是兩個小時。因此,基於上述例子,出租車必須按照正常費率只收取1小時,並根據時間限制收費1小時,迷你胸圍必須收費2小時。 –

+2

暫時將時間轉換爲unix時間戳並採取開始和結束之間的差異可能更容易 – dualed

回答

1

在僞代碼:

$now = time(); // = Unit Time Stamp 
$end = getEndOfTripTime(); // = Unix Time Stamp 
$isnight = isNightRate($now); 

if($isnight) 
{ 
    if(getNextNightRateEnd($now) > $end) 
    { 
    $NightRateSeconds = $end - $now; 
    } 
    else 
    { 
    $NightRateSeconds = getNextNightRateEnd($now) - $now; 
    $DayRateSeconds = $end - getNextNightRateEnd($now); 
    } 
} 
else 
{ 
    // etc. 
} 

我認爲你可以在剩下的填寫!$isnight

您也可以將$now視爲$start= getStartOftriptime())以提前計劃行程。

+0

這不是很有幫助。比方說,軟件擁有者需要在23:30開始出租車的夜間費率,然後客戶租用出租車進行需要50分鐘的旅程,並選擇在23:15開始的旅程。根據你的代碼,如何獲得以正常費率收費的15分鐘以及以夜間收費收費的剩餘35分鐘? –

+1

我沒有看到問題,你在哪裏看到它?順便說一句,「分鐘=秒/ 60」 – dualed