Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:29:06+00:00 2026-06-17T04:29:06+00:00

I have create the class that following, that calculating the time before a time

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:29:07+00:00Added an answer on June 17, 2026 at 4:29 am

    In pseudo-code:

    $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.
    }
    

    I assume you can fill in the rest for !$isnight.

    You could also think of $now as $start (= getStartOftriptime()) to plan trips ahead of time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that is able to create a class that has
I have the following code and I wanted to create a class that encapsulates
I have create a class method that creating a Datasource on the fly in
I have an ExpenseType object that I have created with the following migration: class
I have the following code that creates a serverside object of the xmlhttp class.
I am trying to create a class that implements the IUnknown interface. I have
I have written an apex class that will create a PDF quote and attach
I have this class that have a function to load other classes and create
I have a class that desccends from ListviewItem. When I create an instance of
I have create my own NSOpenGLView class, right now the data that i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.