How to calculate hour:minutes from total minutes? - php

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

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

How to get current time elapsed percentage of today? [duplicate]

This question already has answers here:
Work out the percentage of the day that has elapsed
(5 answers)
Closed 9 years ago.
Im trying to get the current time elapsed percentage of todays time. It can be either javascript or php. How can I do that ?
Use a Date object and some arithmetic. Convert the components of the current day into a equivalent unit (such as seconds), find their sum, and divide that by the number of that unit per day.
For example, the following is a solution in JavaScript.
var d = new Date();
var pctDayElapsed = (d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds() + d.getMilliseconds()/1000)/86400;
Note that this approach piggybacks on the browser's localization. Your result will depend on the browser's timezone.
I would use the current time to calculate the number of seconds that has elapsed today and then divide that by 86400 (the number of seconds in a day), and of course multiply that by 100 to get it from decimal to percent.
Here is an answer in PHP:
$now=time();
$today=strtotime(date("m/d/Y"));
$seconds=$now-$today;
$day=24*60*60;//seconds in a day;
$percent=$seconds/$day*100;
OR
$hours=date('G')*60*60;
$minutes=date('i')*60;
$seconds=date('s');
$day=24*60*60;//seconds in a day;
$percent=($hours+$minutes+$seconds)/$day*100
<?php
$timestamp = time();
$hours = intval(date("G", $timestamp));
$minutes = intval(date("i", $timestamp));
$seconds = intval(date("s", $timestamp));
$proportion_elapsed = ($hours * 60 * 60 + $minutes * 60 + $seconds) /
(24 * 60 * 60);
printf("%0.4F of the day has elapsed.", $proportion_elapsed);
?>
This works as long as there are 86,400 seconds in a day, which may not be true due to Daylight Savings time or leap seconds.
Round down to the beginning of the day by dividing the current time by 86400, then multiply that integer value by 86400. Then take the difference of the current time (in seconds of course) and then divide 86400 into it. Lastly, multiply by 100.
Edit: mod works more efficiently
Pseudocode:
MidnightDays = (TimeInSeconds % 86400) * 86400;
Percentage = (TimeInSeconds - MidnightDays) / 86400 * 100;

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

Add after a certain number in 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;
}

Categories