Home > Software engineering >  calculate the average days in a year with a decimal points
calculate the average days in a year with a decimal points

Time:11-08

I have 66 lessons and I want to get the average number of days between each lesson in a year

I did :

$average = 365 / 66;

The result i'm getting is : 5.530 my problem is how I can use the decimal point .530, otherwise if I use 5 for the average day, say in 30 days, I will have days/months at the end of the year with nothing for example.

I'm looking for a way to use the decimal points so I can have the correct average per year / or also I can make it flexible like having first 3 months with 5 days as average and next 3 months with 6 days average. the important thing is to have no gap at the end of the year.

I tried to use

$average = round(365/66);

but also I'm not getting the correct average, I'm looking for a flexible way to use the decimal points as I said. I can have 5 days for a few months and then 6 days for a months, otherwise please give me a suggestion. Thanks in advance.

CodePudding user response:

If created a little algorithm to compute when the lessons should be held:

$lessons = 66;
$daysPerYear = 365.2425;
$daysBetweenLessons = $daysPerYear / $lessons;
$previousDay = 0;
for ($lesson = 1; $lesson <= $lessons; $lesson  ) {
    $nextDay = floor($daysBetweenLessons * $lesson);
    echo "$lesson => $nextDay (" . ($nextDay-$previousDay) . ")\n";
    $previousDay = $nextDay;
}

This returns:

1 => 5 (5)
2 => 11 (6)
3 => 16 (5)
4 => 22 (6)
5 => 27 (5)
6 => 33 (6)
7 => 38 (5)
8 => 44 (6)
9 => 49 (5)
10 => 55 (6)
11 => 60 (5)
12 => 66 (6)
13 => 71 (5)
14 => 77 (6) 
15 => 83 (6) 
16 => 88 (5)
17 => 94 (6)
18 => 99 (5)
19 => 105 (6)
20 => 110 (5)
21 => 116 (6)
22 => 121 (5)
23 => 127 (6)
24 => 132 (5)
25 => 138 (6)
26 => 143 (5)
27 => 149 (6)
28 => 154 (5)
29 => 160 (6)
30 => 166 (6)
31 => 171 (5)
32 => 177 (6)
33 => 182 (5)
34 => 188 (6)
35 => 193 (5)
36 => 199 (6)
37 => 204 (5)
38 => 210 (6)
39 => 215 (5)
40 => 221 (6)
41 => 226 (5)
42 => 232 (6)
43 => 237 (5)
44 => 243 (6)
45 => 249 (6)
46 => 254 (5)
47 => 260 (6)
48 => 265 (5)
49 => 271 (6)
50 => 276 (5)
51 => 282 (6)
52 => 287 (5)
53 => 293 (6)
54 => 298 (5)
55 => 304 (6)
56 => 309 (5)
57 => 315 (6)
58 => 320 (5)
59 => 326 (6)
60 => 332 (6)
61 => 337 (5)
62 => 343 (6)
63 => 348 (5)
64 => 354 (6)
65 => 359 (5)
66 => 365 (6)

So it alternates 5 and 6 day intervals based on what's behind the decimal point.

  •  Tags:  
  • php
  • Related