PHP Conditional rounding of the minutes of a time stamp - php

I need a Function that rounds the minutes of a time stamp to round up the next 15 minutes when the time is 5 minutes above the previous 15 minutes.
01:05:00 = 01:15:00
01:04:00 = 01:00:00
01:20:00 = 01:30:00
01:19:00 = 01:15:00
I have this right now but the flaw is some times it will round down due to the really poor attempt on my part.
function roundTime10($timestamp10, $precision10 = 10) {
$timestamp10 = strtotime($timestamp10);
$precision10 = 60 * $precision10;
return date('H:i:s', round($timestamp10 / $precision10) * $precision10);
}
$Billable_Time = roundTime10($Billable_Time);
function roundTime15($timestamp15, $precision15 = 15) {
$timestamp15 = strtotime($timestamp15);
$precision15 = 60 * $precision15;
return date('H:i:s', round($timestamp15 / $precision15) * $precision15);
}
$Billable_Time = roundTime15($Billable_Time);
echo $Billable_Time;
could anyone show me a better way to do this or ad to my way to make it work?

The basic logic for this is "subtract five minutes, divide by 15 to an integer result then add one and multiply by fifteen".
01:04:00 = 01:00:00: 4-5 = -1. -1/15 = -0.xxx (round down to -1). (-1+1)*15 = 0
01:20:00 = 01:30:00: 20-5 = 15. 15/15 = 1 (round down to 1). (1+1)*15 = 30
01:19:00 = 01:15:00: 19-5 = 14. 14/15 = 0.xxx (round down to 0). (0+1)*15 = 15
I don't have a PHP interpreter handy so won't write the code but you need to replace "round($timestamp15 / $precision15) * $precision15" with something that uses the logic above. You'll want to use the PHP floor() as it rounds negatives down while round() rounds towards or away from zero.

Related

How to round an integer UP to the nearest 15 in PHP

I have the INTEGER(255) variable $duration stored with a value taken from the user. I need to round this UP to the nearest 15. I have searched all over but haven't been able to find a solution.
How may I go about doing this?
For example:
10 becomes 15
16 becomes 30
130 becomes 135
Also, how can I add $duration to a TIME variable $time to output the time after that duration?
For example, from 080000:
If $duration is 15, $time becomes 081500
If $duration is 30, $time becomes 083000
If $duration is 135, $time becomes 101500
Thank you!
You can use the simple division and addition with casting (will return the whole value without the fraction) operator as follows:
$val = 130;//or any value
$roundedVal = ((int)($val/15) + 1) * 15;
about the second question:
$time='080000';
$valHours = (int)($val/60);
$valMin = (int)($val % 60);
$time = $time + ($valHours * 10000) + ($valMin * 100);
$time = strlen($time) == 8? $time : '0' . $time;
the last row is meant or fixing the leading zero, since it's not a conventional time format.
I haven't took into consideration the seconds.

PHP Get Time From X Percentage of Total Time

I don't know if this will make sense but I would like to get the time from percentage of a value, I have the following as an input example:
mypercentage = 50;
mytime = "00:59:59"
As you can see, my time is 60 minutes (1 hour) and 50% of that is 30 minutes therefore from the 2 above inputs I would like to get the following output
(50 % of an hour):
00:30:00
Explode the string into an array
Convert the 3 elements into numbers
Find the value in seconds. $array[0]*3600+$array[1]*60+$array[2]
Calculate the time from the value in step 3 and the percentage.
Use /3600 and /60 to find the hour and minutes and %60 to find the seconds.
put the values in an array and implode to a string, or just use
$hour.":".$minute.":".$second
Thank you all who responded, I found this solution to work:
$mypercentage = 50;
$mytime = '00:59:59';
$totaltime = date('H:i:s', strtotime($mytime));
$seconds = strtotime("1970-01-01 $totaltime UTC");
$per_seconds = ($mypercentage / 100) * $seconds;
$time_from_percentage = gmdate("H:i:s", $per_seconds);
echo $time_from_percentage;

MySQL & PHP : Time() to Decimal Hour

I have in my MySQL database two Time() saves: start and end.
I would like to have a decimal result of the interval between my two times in my PHP code.
for exemple, if :
start = 15:00:00
end = 16:00:00
result = 1
and if
start = 15:30:00
end = 16:00:00
result = 0,5
Is it possible to get this result using MySQL? If it isn't possible, how could I convert a time format to decimal interval please?
Thanks a lot for reading my issue.
Very straightforward solution would be to take this two values, convert them to time and then make it to hours.
$start = "15:00:00";
$end = "16:00:00";
$result = (strtotime($end) - strtotime($start))/3600;
That gives you $result = 1
For values 15:30:00 and 16:00:00 $result will be 0.5.
Okay,
So I think it is easy. Just follow these steps:
You need to separate those numbers. Make 3 variables. For this example we'll use $hours, $minutes, $seconds (you can achieve these by separating the main time using REGEX. I recommend reading about how to do it first)
Now you have these numbers, for example like so:
$start = "15:30:00";
$start_hours = "15"
$start_minutes = "30"; //or just 0
$start_seconds = "00"; //or just 0
$end = "16:00:00";
$end_hours = "15"
$end_minutes = "00"; //or just 0
$end_seconds = "00"; //or just 0
So... this is briefly how you need to separate those numbers.
Now about the counting...
$result_hours = $end_hours - $start_hours //there you get 1
So hours are easy... For the minutes we need to think about time it self. Cause time values are always in 60 and we need "percents" which are in 100. We'll solve this doing:
$result_minutes = $end_minutes + $start_minutes; //you get 30.. which obviously is NOT what we want.. easily fixed by://
$right_minutes = 100 / 60 * $result_minutes; // gives you 50, which is great.
So far, we have 1,50 (If you'll take enough effort you can simply devide 50 by 10.. you'll get 5)
$result_seconds = $end_seconds + $start_seconds;
$right_seconds = 100 / 60 * $result_seconds;
Now we need to realize that we are dealing with seconds, so the number will be percentage of MINUTES not hours, we can fix this using:
$best_seconds = 100 / 60 * (0.$right_seconds) //now we have all right.
$right = $right_minutes + $best_seconds;
$result = $result_hours.",".$right;
.........................
And that should be it. Hope it will help you figure out the best solution, this is just how I thought it might work.
Try. Change. Try. And Done.
Good Luck

Percentage Increase or Decrease Between timestamps

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;

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

Categories