Add after a certain number in PHP - php

I have this project that has a set price for a certain amount of hours, I need to add more money to the overall total for each hour after 5.
My script already calculated the time by counting the hours:
Which outputs like, "15" or "9.5" or "3.5" or "7" etc.
Let's say that 5 hours is £50, how would I add an additional £15 for every hour over the 5 hour limit.
This includes if a user going over by ".5"
(So 5.5 hours would be £65 and 6 would be £65)
Any help would be great, Thanks!

Subtract 5 from the number of hours, ceil() / round() the number and multiply by 15?
You ceil() if you want any part of an hour to be charged, whereas you round() if you want to charge only if the fraction of the hour is .5 or higher.
$hours = 5.5;
$amount = 50 + ceil(max(0,$hours-5)) * 15; # 65

I assume that anything belove 5 hours is 50.
$price = $val <= 5 ? 50 : (50 + (int) ceil($val-5) * 15)

Assuming I've understood you correctly, are you looking for something like this:
$total = 50;
if($hours > 5) {
$total += ceil($hours - 5) * 15;
}

Related

PHP: display 90 min as 1.5 hours

Minutes=90
Need to display in hours
echo intdiv($ct['time'], 60).'.'. ($ct['time'] % 60)." Hours";
The output is 1:30 hours.
But i need to display as 1.5 hours.
How to convert the minute into hours? As 1.5, 1.6, 1.7, 1.8, 1.9, 2 hours and so on .
I agree with all the comments saying that it will be clearer to display 1h20 than 1.33 hours. You may could round it and display 1 h and ½ or 1 hour and ¼ but still, it's not easy to read.
If you want to stick to your hours then you could do this:
$min = $cat['prepration_time'];
if ($min < 60) {
echo $min == 1 ? "$min minute" : "$min minutes";
}
else {
$hours = round($min / 60, 1);
echo $hours == 1 ? "$hours hour" : "$hours hours";
}
Examples of output:
1 minute
20 minutes
50 minutes
1 hour
1.2 hours
1.5 hours
1.7 hours
It appears that you have one variable that stores an amount of minutes, $cat['prepration_time'] in your case. To output the minutes as decimal hours all you really need to do is to divide the number of minutes by 60, that's the number of minutes in one hour. In your case that would be
echo $cat['preparation_time'] / 60 . ' hours';
To avoid awkward results like 1.33333333333 hours, you can additionally wrap the calculation in round, where the second parameter of round is the precision, i.e. the number of digits behind the dot.
$minutes = 77;
echo round($minutes / 60, 2) . ' hours'; // 1.28 hours
Be aware however that this format is badly readable and prone to misunderstanding. Does 1.30 hours mean 1 hour and 30 minutes or rather 1 hour and 18 minutes? And while you might be able to convert 1.30 hours in your head, you will surely need a calculator for 1.283 hours. I would recommend to use a commonly understood and unambiguous format, like for example 1 hour and 32 minutes, or 1:32 hours.
Add this code.
Firstly add the hour and minutes after that I have added round function so it will show proper output upto 2 decimal.
$hours =1; $minutes=30 ; $CalculateHours = $hours + round($minutes / 60, 2); echo $CalculateHours . " hours";
Result: 1.5 Hours

Calculate hour and pay

ok im stuck at how to calculate the pay. for example how would i calculate 14 hours and 42mins * Pay Salary
i tried this 14.42 * 15 = 216.3 but im pretty sure thats off by a few cents or dollars. Any idea how to go about it
Since there are 60 minutes in an hour, and not 100, 14.42 isn't 14 hours and 42 mins, but rather 14 hours and 25 minutes.
To get the proper number in hours, you should be doing: $hours + ($minutes / 60).
However, DON'T DO THAT.
If you follow this logic, you are making calculations with floats, which can lead to unexpected results.
ALWAYS use the smallest integer unit. In your case, you want to do the calculation with minutes AND cents of dollars (and not dollars!)
For example:
$worktime = $hours * 60 + $minutes;
$salary = 1540; // = $15.40 -> in cents!
$pay = $worktime * $salary; // This result is in cents
You will always store the integer $pay (cents value) in the database (in an integer column!), and use it for calculations. You will display it in dollar, with decimals, only at the end, while displaying the result on screen.
You're forgetting that 1h = 60m, so doing 14.42 is incorrect, since 14h42m should be 14 + (42/60) = 14.7 hours.
This would mean that you need to do:
14.7 * 15 = 220.5

Creating an inflation calculator in PHP

Heres the code I have at the moment, its all working as intended, however, the cumulative total isn't working, and I'm positive I'm doing something absolutely stupid.
assume period = 20
assume inflation = 3
assume nightlycost = 100
assume nights = 7
$yearlycost = $nightlycost*$nights;
while ($period > 0) {
$period = $period-1;
$yearlyincrease = ($inflation / 100) * $yearlycost;
$nightlyincrease = ($inflation / 100) * $nightlycost;
$nightlycost = $nightlycost + $nightlyincrease;
$yearlycost = ($yearlycost + $yearlyincrease) + $yearlycost;
}
Result:
Nightly Hotel Rate in 20 years: $180.61 - <?php echo round($nightlycost, 2); ?> correct
Weekly Hotel Rate in 20 years: $1264.27 - <?php echo round($nightlycost, 2) * 7; ?> correct
Total cost to you over 20 years: $988595884.74 - <?php echo round($yearlycost, 2); ?> incorrect
Everything outputs correctly and as expected, except for the yearly cumulative cost. It should take the previous yearly cost and add that years cost+inflation.
Example: first year is 700, so second year should be 700 + 700 + 21 (21 is 3%, the inflation for that year). Second year cumulative total is thus: 1421. Third year will be 1421 + 721 (last years total) + 3% of 721.
Hopefully this is clear enough for you to see where I'm going wrong. Thanks!
I find it hard to understand where your code goes wrong, but my intuition is that the last line in your loop body should have a multiplication.
Basically, you have a base cost for period 0. Then you want to calculate the cumulative cost given inflation after X years. That cost is (pseudocode)
base = nightlycost + nights
infl = 1.03
cumulative = base + base*infl + base*infl^2 + base*infl^3 + ... + base*infl^periods
The last expression can be simplified to
cumulative = base*((1-infl^periods)/(1-infl))
(This holds according to Eq. 4 here: http://mathworld.wolfram.com/ExponentialSumFormulas.html)
Example:
$base = 100*7;
$infl = 1.03; // 3% of inflation/year
$periods = 2;
$cumulative = $base * (1-pow($infl, $periods))/(1-$infl);
print "Cumulative cost after $periods is $cumulative\n";
// Let's try with three periods.
$periods = 3;
$cumulative = $base * (1-pow($infl, $periods))/(1-$infl);
print "Cumulative cost after $periods is $cumulative\n";
Output:
Cumulative cost after 2 is 1421
Cumulative cost after 3 is 2163.63

PHP 15 minute rounding

In PHP I need to do a count of 15 minutes within a number of hours and minutes. But I need it to round down if less or equal to 7 minutes and round up if over 7 minutes.
So I have something like;
$totalHours = 10;
$totalMinutes = 46;
The hours is easy enough to work out the number of 15 minutes;
$totalHours = 10*4;
But the minutes is giving me some grief.
Some examples are;
If Minutes = 7 then answer is 0.
If Minutes = 8 then answer is 1.
If Minutes = 18 then answer is 1.
If Minutes = 37 then answer is 2.
If Minutes = 38 then answer is 3.
I would be really grateful if someone could do me a nice function for this. A signature of (int TotalHours, int TotalMinutes) would be great.
Thanks,
Mike
Try this code:
<?php
echo round($n/15);
?>
// where n is your minute number i.e. 7, 8, 18, 37, 38 etc
Calculate the number of minutes as (60 * hours + minutes), then add 8, divide by 15, and truncate the result by a call to int().
To round to the nearest X: Divide by x, round to nearest int, multiply by x.
$nearest = round($minutes / 15) * 15;
But in your case, if you just want a count of that, you can simply skip the last step.
$numberOfQuarterHours = round($minutes / 15);
You could try modulo operation.
PHP.net MOD
Just divide by 15 and get the remaining part. There you could apply test for 7 < x < 8

How to calculate hour:minutes from total minutes?

For example i have 525 minutes, if we will divide it by 60 the result will be 8.75
But 1 hour have only 60 minutes not 75
How can i calculate the exact hour:minutes from total minutes?
$hours = intval($totalMinutes/60);
$minutes = $totalMinutes - ($hours * 60);
Edited to be PHP
This kind of conversion is done using integer division and the modulo operator. With integer division you find out how many of the "large" unit you have and with modulo you find out how many of the "small" unit are left over:
define('MINUTES_PER_HOUR', 60);
$total_minutes = 525;
$hours = intval($total_minutes / MINUTES_PER_HOUR); // integer division
$mins = $total_minutes % MINUTES_PER_HOUR; // modulo
printf("%d minutes is really %02d:%02d.\n", $total_minutes, $hours, $mins);
See it in action.
floor(525 / 60) gives the number of hours (8.75 rounded down to 8).
525 % 60 gives the number of minutes (modulo operator).
What I did for my girl friend made her chart with 60 min intervals eg 1=60,2=120,3=180,4=240,5=300,6=360 etc etc. then I told her to get her minutes eg 337 find the closest number without going over that would be 5 then use the number 5 equals and subtract it from your original minutes 337-300=37 the remainder is the minutes thus 337 minutes equals 5 hours and 37 minutes

Categories