时间计算类问题


Time calculation class issue

我已经创建了下面的类,计算时间框架之前的时间,时间框架内的时间,以及时间框架旁边的时间。

更具体地说,我创建了一个出租车预订系统。 这辆出租车白天正常收费,晚上双倍收费。

网站所有者,有能力设置的时间,夜间费率开始和结束。例如,正常的出租车夜间收费00:0005:00,而小巴出租车夜间收费从23:0006:00

在客户端下单时,Google地图计算行程持续时间,所以我们也设行程为两小时。

基于此场景,在出租车的情况下,终端用户必须按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开始,所以结果应该是Day: 0 night: 7200

最后,请注意,我在这行中应用了一些修改:

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

因为我认为问题出在这条线上。遗憾的是,我还没有找到任何解决办法。

伪代码中:

$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())来提前计划行程。