U
U
Username2015-04-18 21:05:44
PHP
Username, 2015-04-18 21:05:44

How to hang a two-week schedule on the calendar of events?

Good evening. You need to adapt your two-week schedule to the calendar on the site, i.e. the user can click on any day of the week and the schedule for that day will be displayed in the ajax window.
And the question is how to make the alternation of weeks from the 1st to the second, where the information will be stored, it is clear that the schedule itself is in the database, but what parameter is responsible for determining what week it is when changing the month, year?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2015-04-19
@dalv_happy

So if you store data in the database, then use the functions of the database. The number of the week in the year is a standard function of the same MySQL. Be attached to the parity of the week, here will be the first or second.

E
Evgeny Petrov, 2015-04-18
@Petroveg

If I understood the question correctly, then something like this.
Suppose the year, month and date are known (if the current date, then the values ​​of these variables can be obtained from new Date() using the appropriate methods).
The variable start contains the month and date of the beginning of the school year.
Then:

/*Задаём начало учебного года, месяц задан в человеческом восприятии*/
var start = [9, 1];

function getIndex (date, month, year) {
  /*Исправляем возможную неточность в месяце*/
  month = month % 12;
  /*Если год не получен, берём из текущей даты*/
  year = year || new Date().getFullYear();

  var currentDay = new Date(year, month - 1, date),
    /*Узнаём начало текущей недели*/
    startWeekDay = date - (currentDay.getDay() + 6) % 7,
    /*Определяем год начала учебного года*/
    startYear = month < start[0] ? year - 1 : year,
    /*Получаем разницу между началом текущей недели и началом учебного года*/
    delta = new Date(year, month - 1, startWeekDay) - new Date(startYear, start[0] - 1, start[1]);

  return Math.ceil(delta / (7 * 1000 * 3600 * 24)) % 2 + 1;
}

console.log(getIndex(30, 4, 2015)); //30 апреля 2015 года
console.log(getIndex(1, 5, 2015)); //1 мая 2015 года
console.log(getIndex(4, 5, 2015)); //4 мая 2015 года — начало новой недели
console.log(getIndex(4, 5)); //4 мая текущего года

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question