开发者

php: round minutes up to the nearest quarter hour, then do more

The initial problem is this: Take the amount of minutes -> turn into quarter hours -> 1 quarter hour is 1 unit -> output units

I've been putting a page together all day today and my brain just stopped working a couple of minutes ago and I just can't wrap my head around how to output the amount of units. I knew posting the problem on this site would help.

So the user puts in the amount of minutes (not hours and minutes, just minutes) and the site needs to output the amount of units. A unit is a quarter hour. T开发者_如何学JAVAhe minutes are always rounded up to the nearest quarter hour. I know I'll use ceil in some sort of way and probably(?) number_format, but I just can't get it to come out correctly. Any help is appreciated.


Try:

$units = ceil($minutes / 15) 


My take on this:

return (round($minutes / 15) * 15) % 60;

Produces:

for($i=0; $i<60; $i++) {
    echo $i . ' -> ' . roundQuarter($i) . '<br/>';
}

function roundQuarter($minutes) {
    return (round($minutes / 15) * 15) % 60;
}

0 -> 0
1 -> 0
2 -> 0
3 -> 0
4 -> 0
5 -> 0
6 -> 0
7 -> 0
8 -> 15
9 -> 15
10 -> 15
11 -> 15
12 -> 15
13 -> 15
14 -> 15
15 -> 15
16 -> 15
17 -> 15
18 -> 15
19 -> 15
20 -> 15
21 -> 15
22 -> 15
23 -> 30
24 -> 30
25 -> 30
26 -> 30
27 -> 30
28 -> 30
29 -> 30
30 -> 30
31 -> 30
32 -> 30
33 -> 30
34 -> 30
35 -> 30
36 -> 30
37 -> 30
38 -> 45
39 -> 45
40 -> 45
41 -> 45
42 -> 45
43 -> 45
44 -> 45
45 -> 45
46 -> 45
47 -> 45
48 -> 45
49 -> 45
50 -> 45
51 -> 45
52 -> 45
53 -> 0
54 -> 0
55 -> 0
56 -> 0
57 -> 0
58 -> 0
59 -> 0


$rounded = floor($minutes / 15);

1 minute / 15 = 0.0666 -> 0
14 minute / 15 = 0.9333 -> 0;
15 / 15 = 1 -> 1;

This'll give you 0->3 for the range of 15 minute periods. If you want 1-4, then use ceil() instead.


Does this not do the trick?

$numberOfMinutes = 135;

$numberOfFifteenMinuteUnits = round($numberOfMinutes / 15);

echo $numberOfFifteenMinuteUnits;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜