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
Related
This supposed to be easier to solve or google the answer, but I just can't get it well done. May be I'm just stuck:
This is what I tried:
$now = time();
// i.e Improve police arriving time from 15 mins to 10 mins
$array_ini = explode(':',$ini_value); // "00:15:00" in my example (15 mins)
$array_desired = explode(':',$desired_value); // "00:10:00" in my example
$ini = $now-mktime($array_ini[0],$array_ini[1],$array_ini[2]);
$des = $now-mktime($array_desired[0],$array_desired[1],$array_desired[2]);
$percent = (1-$ini/$des)*100;
But all I get is .47% as improvement and my logic says that it really is a 33% improvement.
What am I doing wrong?
It's much easier to just deal with minutes:
$ini_mins = 15;
$desired_mins = 10;
$improvement_mins = $ini_mins - $desired_mins;
$percent = ($improvement_mins / $ini_mins) * 100;
print_r($percent);
It is indeed straight forward and easier to just deal with minutes, as Ryan said in his answer.
But to add to it, what you are doing wrong - you are deducting unix time of 01 Jan 1970 00:10:00 and 01 Jan 1970 00:15:00 from say unix time of 15 Sep 2015 19:00:00. Of course the percentage difference between these two numbers would be small. You are doing something like this
num1 = 100000 - 10
num2 = 100000 - 15
percentage num1/num2 is wrong way to find percentage diff between 10 and 15; and also it is going to be much smaller than 33%.
plus you have a code bug. The array is called $array_desired but you reference $array_des in mktime.
Ok. I guess that Amit opened my mind.
Based on his comment, I post the right answer:
// This line is not needed any more
//$now = time();
// i.e Improve police arriving time from 15 mins to 10 mins
$array_ini = explode(':',$ini_value); // "00:15:00" in my example (15 mins)
$array_desired = explode(':',$desired_value); // "00:10:00" in my example
// Time must to be based on Jan, 1 1970
// Hours are from 1 to 23, so must be increased by 1
$ini = mktime($array_ini[0]+1,$array_ini[1],$array_ini[2],1,1,1970);
$des = mktime($array_desired[0]+1,$array_desired[1],$array_desired[2],1,1,1970);
$percent = (1-$des/$ini)*100;
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
I'm trying to make a script which penalizes a user daily after a time stamp.
For the first day, it will penalize 1 point, second day 2 points, third 4 points, fourth 8 point, 16, 32, 64 and so on.
How would I go about auto generating a strtotime and the multiplications?
I really don't even know what i'm looking for is called at this point which makes searching hard, sorry if this has been posted.
Let's suppose, that $timestamp1 is the first day timestamp and the $timestamp2 is now timestamp. Then:
$difference = abs(strtotime($timestamp2)-strtotime($timestamp1));
$days = floor($difference / (60*60*24));
$penalty = pow(2,$days);
echo "{$days} left, so your penalty is: {$penalty}";
Sample results:
0 left, so your penalty is: 1
1 left, so your penalty is: 2
2 left, so your penalty is: 4
3 left, so your penalty is: 8
4 left, so your penalty is: 16
5 left, so your penalty is: 32
6 left, so your penalty is: 64
...
Try this
$initial_date = '2012-10-20';
$initial_datetime = new DateTime($initial_date);
$today_datetime = new DateTime();
$days_passed = $initial_datetime->diff($today_datetime)->format('%a');
$penalty = pow(2, ($days_passed-1));
echo $penalty;
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
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;
}