-3

I'm building a sort of a calendar application in Laravel, PHP, Jquery. The user can set recurring events.

On daily events it's ok, they can choose the day of the week they want the event to happen. On Monthly events, also ok, they can choose the months that the event will reocurr.

On Weekly events, there is a problem: Since we don't really track weeks in the calendar, I can't really figure out how to make it recurr.

My first thought was to figure out the week of the month, and then let them choose which ones they want it to happen. Won't work because months have different number of weeks - some happen in 4 weeks, some in 5 and some (like this one) in 6 weeks. The code for getting the week of the month I'm using:

Date.prototype.getMonthWeek = function(){
    var firstDay = new Date(this.getFullYear(), this.getMonth(), 1).getDay();
    return Math.ceil((this.getDate() + firstDay)/7);
}

$("#result").html("Today is week " + new Date().getMonthWeek() );

Then my idea was to get even or odd weeks, from a specific date. This would limit me to set events to recurr every week or every two weeks but would work ok, until i figured the user can't really tell which week is even or odd aside from creating an event, testing if it reocurrs.

Then, getting the week of the year. Even weeks vs. odd weeks. But if the year has an odd number of weeks, it would happen twice in january.

I'm looking for the simplest way to make it happen and I'm really out of brainpower on this, any possible solutions?

sigmaxf
  • 7,998
  • 15
  • 65
  • 125
  • if you want to know the week of the year http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php – Bertrand Martel Aug 13 '15 at 13:41
  • I don't understand the CS problem here. It should be no problem at all to get a week number from a date using some standard date library. Also, I don't understand your problem with twice in January: An event at odd weeks will always be at the 1st and 3rd week of January, no matter how many weeks the year has. – dirkk Aug 13 '15 at 13:42
  • If I get the number of weeks in a year, and, let's say the year has 67 weeks, and it will happen on odd weeks, then will happen on 67 and 1 again. Won't be every 2 weeks – sigmaxf Aug 13 '15 at 13:46
  • Same for months that have 5 weeks – sigmaxf Aug 13 '15 at 13:46

1 Answers1

0

Couldn't you use something like PHP's date function? That has an option to select the week of the year.

If you'd like to go the js route, I highly recommend moment.js and you could do something like: moment().add(‘weeks’, 1)

JasonJensenDev
  • 2,377
  • 21
  • 30