我创建了跟随的类,即计算时间范围之前的时间,时间范围内的时间以及时间范围旁边的时间。
更具体地说,我创建了一个出租车预订系统。出租车一日游收费正常,夜间旅行收取双倍费用。
网站所有者,可以设置夜间费率开始和结束的时间。举个例子,假设正常的出租车夜间费率从 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 < / p>
最后,请注意我已在行中应用了几项修改:
if(($current_hour >= $this->from_hour && $current_hour < $this->to_hour))
因为我认为这个问题就是这个问题。不幸的是我找不到任何解决方案。
答案 0 :(得分: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()
)来提前计划行程。