Hey all,

Well I can't find a common solution for calculating price based on time elapsed and I thought you guys could help me a bit to find an appropriate solution.

So, I need to calculate price like this:
24h - 40€
30h - 50€
36h - 60€
42h - 70€
48h - 80€, so this is it basically for 2 days and the rest should be like this
+8 remaining days the price is 40€ per day plus 2 days elapsed 80€ and for 10 following days price should be 30€ per day, and the rest +20 days and on price should be 20€ per day

Quit complex, isn't it!!

Thanks in advance,
mightysnake

You are expected to make your best attempt at coding the solution and then posting some code if you have a problem. It isn't code-on-demand where you can just post a requirement. This isn't a very difficult requirement so if you have some programming skills, it shouldn't be a problem. If you aren't a programmer, then you should be posting in some other forum looking for someone to code for you.

Well here's the code

$registered_date = substr($rw['p_datetime_created'],0,10);
		$registered_time = substr($rw['p_datetime_created'],11,8);
		$registered_time = substr($registered_time,0,5);
		
		$today_date = date("Y-m-d");
		$today_time = date("H:i");
		
		$start = strtotime($registered_date." ".$registered_time);
		$stop = strtotime($today_date." ".$today_time);

		$difference = $stop - $start;
		$hours = round($difference / SECONDS_PER_HOUR, 0, PHP_ROUND_HALF_DOWN);
		$minutes = ($difference % SECONDS_PER_HOUR) / 60;
		
		$case_value = $rw['p_type'];

Well, this is the code for calculating hours and minutes and the solution with switch statement, but this is an older version of calculating prices

switch($case_value)
{
	case "1":
		$total_price = ($hours <= 24) ? 40 : round((1.667 * $hours),0);
		$registration_type = "Type 1";
		break;
	case "2":
		$total_price = ($hours <= 24) ? 10 : round((0.417 * $hours),0);
		$registration_type = "Type 2";
		break;
	case "3":
		$total_price = ($hours <= 24) ? 10 : round((0.417 * $hours),0);
		$registration_type = "Type 3";
		break;
	case "4":
		$total_price = ($hours <= 24) ? 0 : round((0 * $hours),0);
		$registration_type = "Type 4";
		break;
}
?>

So, basically it checks whether the first condition is true else it calculates hours * 1.667€ per hour. But the price table has changed a bit so this is the part where I'm getting confused

You can solve this with a series of if statements by working backwards from greatest amount of time to least amount of time. Have a variable store how much time is remaining to be added to the total price.

Pseudocode:

tempTime = totalTime
price = 0

if tempTime is over 20 days
   calculate time over twenty days, multiply by rate and add to price
   subtract time over twenty days from tempTime

if tempTime is over 10 days
   calculate time over ten days, multiply by rate and add to price
   subtract time over ten days from tempTime

if tempTime is over ... etc. for each scheduled tier.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.