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开始,所以结果应该是 Day: 0 Night: 7200

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

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

因为我相信问题是这条线。不幸的是,我还没有找到任何解决方案。

4

1 回答 1

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

于 2013-01-09T10:05:09.327 回答