I have create the class that following, that calculating the time before a time frame, the time inside the time frame, and the time next to time frame.
More specific, I creating a taxi booking system. The taxi has normal charges for day trips, and double charges for night trips.
The web site owner, has the ability to set the time’s that night rate starts and ends. As an example let’s say that normal taxi night rates start at 00:00 and ends at 05:00, and the night charges for mini bus taxi, start at 23:00 and ends at 06:00.
At the time that the client makes the order, the Google maps calculating the trip duration, so let’s say also that the trip is two hours long.
Based on this scenario, in case of taxi the end user must be charged with 1 hour of normal rate and 1 hour with double rates. In case of mini bus the end user must be charged 2 hours in double rate, while the night rates for mini bus starts at 23:00.
With my class, in its current state the first example works fine, but the second is wrong and I cannot find the reason.
<?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;
}
}
}
?>
Based on the examples above let’s say I have the following code:
<?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 />';
?>
End the result of the above code is the following:
TAXI NIGHT DURATION
Day : 3600
Night : 3600
MINI BUS NIGHT DURATION
Day : 3600
Night : 3600
As you can see the above results are wrong, because the minibus night rate starts at 23:00, so the result should be Day: 0 Night: 7200
Finally, note that I have apply several modifications in the line:
if(($current_hour >= $this->from_hour && $current_hour < $this->to_hour))
Because I believe that the issue is this line. Unfortunately I have not find any solution.
In pseudo-code:
I assume you can fill in the rest for
!$isnight.You could also think of
$nowas$start(= getStartOftriptime()) to plan trips ahead of time.