Note that there are some explanatory texts on larger screens.

plurals
  1. POTime calculation class issue
    primarykey
    data
    text
    <p>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.</p> <p>More specific, I creating a taxi booking system. The taxi has normal charges for day trips, and double charges for night trips.</p> <p>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 <strong>taxi night rates</strong> start at <strong>00:00</strong> and ends at <strong>05:00</strong>, and the night charges for <strong>mini bus</strong> taxi, start at <strong>23:00</strong> and ends at <strong>06:00</strong>.</p> <p>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.</p> <p>Based on this scenario, in case of <strong>taxi</strong> the end user must be charged with <strong>1 hour</strong> of normal rate and <strong>1 hour</strong> with double rates. In case of <strong>mini bus</strong> the end user must be charged <strong>2 hours</strong> in <strong>double rate</strong>, while the night rates for mini bus starts at <strong>23:00</strong>.</p> <p>With my class, in its current state the first example works fine, but the second is wrong and I cannot find the reason.</p> <pre><code>&lt;?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-&gt;start_hour = date('H', $start_date); $this-&gt;start_minute = date('i', $start_date); $this-&gt;duration = ($duration / 60); // Convert seconds to minutes $this-&gt;from_hour = date('H', $night_start); $this-&gt;from_minute = date('i', $night_start); $this-&gt;to_hour = date('H', $night_end); $this-&gt;to_minute = date('i', $night_end); $this-&gt;calculate(); } private function calculate() { $this-&gt;getHoursMinutes(); $current_hour = $this-&gt;start_hour % 24; $is_first_loop = true; for($minute = 0; $minute &lt; $this-&gt;duration; $minute++) { $current_minute = ($this-&gt;start_minute + $minute) % 60; if($current_minute == 0 &amp;&amp; $is_first_loop == false) { $current_hour = ($current_hour + 1) % 24; } else if($current_minute == 59) { $is_first_loop = false; } if(($current_hour &gt;= $this-&gt;from_hour &amp;&amp; $current_hour &lt; $this-&gt;to_hour)) { $this-&gt;night_duration++; } else { $this-&gt;day_duration++; } } } private function getHoursMinutes() { $this-&gt;totalHours = round($this-&gt;duration / 60 / 60, 0); $this-&gt;totalMinutes = round($this-&gt;duration / 60, 0); $this-&gt;extraMinutes = round(($this-&gt;duration / 60) % 60, 0); } public function getDayDuration($inSeconds = true) { if($inSeconds == true) { return $this-&gt;day_duration * 60; } else { return $this-&gt;day_duration; } } public function getNightDuration($inSeconds = true) { if($inSeconds == true) { return $this-&gt;night_duration * 60; } else { return $this-&gt;night_duration; } } } ?&gt; </code></pre> <p>Based on the examples above let's say I have the following code:</p> <pre><code>&lt;?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&lt;br /&gt;"; echo "Day : " . $taxi-&gt;getDayDuration() . '&lt;br /&gt;'; echo "Night : " . $taxi-&gt;getNightDuration() . '&lt;br /&gt;'; $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 "&lt;br /&gt;MINI BUS NIGHT DURATION&lt;br /&gt;"; echo "Day : " . $miniBus-&gt;getDayDuration() . '&lt;br /&gt;'; echo "Night : " . $miniBus-&gt;getNightDuration() . '&lt;br /&gt;'; ?&gt; </code></pre> <p>End the result of the above code is the following:</p> <pre><code>TAXI NIGHT DURATION Day : 3600 Night : 3600 MINI BUS NIGHT DURATION Day : 3600 Night : 3600 </code></pre> <p>As you can see the above results are wrong, because the minibus night rate starts at <strong>23:00</strong>, so the result should be Day: <strong>0 Night: 7200</strong></p> <p>Finally, note that I have apply several modifications in the line:</p> <pre><code>if(($current_hour &gt;= $this-&gt;from_hour &amp;&amp; $current_hour &lt; $this-&gt;to_hour)) </code></pre> <p>Because I believe that the issue is this line. Unfortunately I have not find any solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload