4
4
4sadly2020-08-26 17:50:03
PHP
4sadly, 2020-08-26 17:50:03

What's the best way to make a calendar?

It is necessary to make a table where it will be possible to see the number of applications and their total number by day
Table:
Applications 4 11 9 7 ... 31
How best to generate it and send it to the template through the controller?
I can get the number of days and the day of the week myself, there may be more lines, for example, the number of new users
I can’t figure out how to make the code not repeat on each line

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kocherman, 2020-08-26
@kocherman

How to type - you choose. And I can give a short universal calendar script.
Easy to use: 2 parameters: year (for example, 2020), month: (for example, 8 (August)); the output is an array of arrays (weeks - days):

function cal(y,m) {
  let[t,r,d,w,l]=[v=>~~v,[],1,0,y%4],c=(t(23*m/9)+(m<3?y--:y-2)+5+t(y/4)-t(y/100)+t(y/400))%7;
  for(;d<29+(62648012+16*!l>>m*2&3);++d,c=++c%7,w+=!c)(r[w]||(r[w]=[,,,,,,,]))[c]=d;return r
}

console.log(cal(2020,8));
[
  [   ,   ,   ,   ,   ,   ,  1 ],
  [  2,  3,  4,  5,  6,  7,  8 ],
  [  9, 10, 11, 12, 13, 14, 15 ],
  [ 16, 17, 18, 19, 20, 21, 22 ],
  [ 23, 24, 25, 26, 27, 28, 29 ],
  [ 30, 31,   ,   ,   ,   ,    ]
]

Version with the first day of the week being Monday:
function cal(y,m) {
  let[t,r,d,w,l]=[v=>~~v,[],1,0,y%4],c=(t(23*m/9)+(m<3?y--:y-2)+5+t(y/4)-t(y/100)+t(y/400))%7||7;
  for(;d<29+(62648012+16*!l>>m*2&3);++d,c=++c%7||7,w+=!(c-1))(r[w]||(r[w]=[,,,,,,,]))[c-1]=d;return r
}

console.log(cal(2020,8));
[
  [   ,   ,   ,   ,   ,  1,  2 ],
  [  3,  4,  5,  6,  7,  8,  9 ],
  [ 10, 11, 12, 13, 14, 15, 16 ],
  [ 17, 18, 19, 20, 21, 22, 23 ],
  [ 24, 25, 26, 27, 28, 29, 30 ],
  [ 31,   ,   ,   ,   ,   ,    ]
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question