2

I am creating a script for a client of mine, that has a charge of "x" from 5:00 the morning until 24:00 the night. Then from 12:00 to 5:00 the morning, the charge is "y".

Let's say, the charges are the following:

05:00 to 24:00 -> 1.00 $
24:00 to 05:00 -> 2.00 $

and a client buy a service at 23:00. The purchased service require two hours to be completed, so I like to change the charge for the expensive period like that:

The service is total 2 Hours long, and it starts at 23:00, so the charge must be as following:

23:00 to 24:00 = 1 Hour x 1.00 $
24:00 to 01:00 = 1 Hour x 2.00 $

Total : 3.00 $

Also the clients are able to set the start time manualy of the service manualy, at the time of purchase.

The question is, how can I calculate the time in the low cost period and the time in the expensive time period ?

Any idea will be highly appreciated :)

PN: Also must be considered the case that a client get a service at 23:00 and the service will finish at the 06:00. That means that the client must be charged for 2 hours in low cost and 5 hours in high rate.

Kind regards Merianos Nikos

1
  • 2
    Take a look at PHP's date() function Commented Jan 2, 2013 at 17:53

2 Answers 2

3

You just need to know about time boundaries. I don't think you even need date, other than perhaps getting the current server-time.

You need to look at the boundaries of the charge slots.

This seems to work:

function get_charge($starttime, $proctime) {

    $slots = array(
        array(
            "begin" => 5,
            "end" => 24,    // any number larger than 23 will do
            "charge" => 1
        ),
        array(
            "begin" => 0, // 0 is 24, there will never be 24 (like on a clock)
            "end" => 5,
            "charge" => 2
        )
    ); // make sure you have no overlapping slots, and no missing slots either

    $charge = 0;
    for ($i = 0; $i < $proctime; $i += 1) { // get the charge for each hour
        $time = ($starttime + $i) % 24; // modulo 24 here so we go from 0 to 23
        foreach($slots as $slot) {
            if ($slot["begin"] <= $time && $time < $slot["end"]) {
                $charge += $slot["charge"];
            }
        }
    }

    return $charge;
}

var_dump(get_charge(23, 2)); // 3
var_dump(get_charge(7, 2)); // 2
var_dump(get_charge(1, 3)); // 6
var_dump(get_charge(23, 50)); // 50 hour job, because we can
Sign up to request clarification or add additional context in comments.

3 Comments

This might be tough if they want more than 2 charge rates. If it's supposed to be user editable anyway.
The clients can't change the charge slots I would assume. Adding more charge slots should be easy enough, just make sure they don't overlap or leave a gap. You could even modify it to work with more granular timeslots: half hours, minutes or even seconds. The concept remains the same, the total charge is the combined charge for each 'step'.
I read Also the clients are able to set the start time manualy of the service manualy, at the time of purchase. so I assumed that they could create/update time slots. I think I just skimmed too fast. I do like your approach however.
3

Try something like this:

<?php

$currentHour = date('G'); //gets the Hour of the day 24hour format 0-23
$currentMin = date('i'); //gets the minute
if ($currentHour > 5 && $currentHour < 23) {
    //code to charge X
}
//repeat for other times
//Probably use a external DB or file to track the times/rates

?>

3 Comments

Thanks @kevingreen. I already using a database table to track the rates for the time periods :)
yeah, that code is pretty basic, but I think you get the gist ? You can also do the work in SQL too, have it look up records for rate in between time periods too. SELECT rate FROM rates WHERE time>=NOW() AND time<=NOW()
Eh, actually there's a little more involved in the SQL, but that's the idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.