开发者

How to repeat a meeting or event same day of every month

I am trying to create new module for my application where the user can repeat event monthly when they create an event. The problem I am facing is to repeat the 开发者_高级运维event same day of every month. What I wrote works fine. But does not repeat same day of that week of every month. For an example if I create an event 7 July 2011 which is Thursday and If I choose repeat monthly it should create next event on 4 Aug 2011 which is also Thursday. But I could not find a way to repeat at the same day of month. It only works for the same date such as if I choose 7 July, it will create 7th August. I can't think of a way to check the day. Please find my query below.

INSERT INTO EVENT(name, date, usr, repfrequent) VALUES ('test', DATE_ADD('2011-8-7', INTERVAL 1 MONTH), 'Monthly');

The above query repeats the event same date of next month instead of the day such as Thursday of next month. Please let me know if any one knows the answer.


First, IMHO, You'll have to answer the "most important question".

What You mean by monthly?

Sounds maybe stupid, but, I think, You don't want Your events to be repeated monthly.

Scenario1

You want them repeated every 4 weeks. Why do I think so? Because the WEEKDAY is so important for You, not the DAY OF MONTH, and because this is the fastest solution. If this is the case, don't use months, use 4 weeks as a month interval:

<?php
//
date_default_timezone_set('Europe/Berlin');

//
$startDate = '2012-01-06'; // friday

// Show event dates for next 2 years
// one year = ~52 weeks
// 52 weeks / 4 weeks intervals = 13 months ;)
// So, two years = 26 intervals with 4 weeks
//
for($i = 1; $i <= 26; $i++)
{
    $weekOffset = $i * 4;
    $nextDate = strtotime("{$startDate} +{$weekOffset} weeks");
    echo date('Y-m-d l', $nextDate) . PHP_EOL;
}
?>

Result:

2012-02-03 Friday
2012-03-02 Friday *
2012-03-30 Friday *
2012-04-27 Friday
2012-05-25 Friday
2012-06-22 Friday
2012-07-20 Friday
2012-08-17 Friday
2012-09-14 Friday
2012-10-12 Friday
2012-11-09 Friday
2012-12-07 Friday
2013-01-04 Friday
2013-02-01 Friday
2013-03-01 Friday *
2013-03-29 Friday *
2013-04-26 Friday
2013-05-24 Friday
2013-06-21 Friday
2013-07-19 Friday
2013-08-16 Friday
2013-09-13 Friday
2013-10-11 Friday
2013-11-08 Friday
2013-12-06 Friday

As You can see, there will be situations, when an event will occur twice a month (marked with *). If You can accept this, do it this way.

Scenario 2

You want them repeated every 1st|2nd|3rd|4th monday|tuesday|wednesday|thursday|friday|saturday of month, and the day of month (1-31) is unimportant.

And that's why I suggest to use the first scenario. Because, what if I create an event on the 5th (!) Sunday of a month? Just take a look in Your calendar, for example January 2012 has 5 sundays, what do You want Your app to do, when I create an "monthly" event on 2012-01-29, it's a sunday, The 5th sunday of January 2012. February 2012 has only four sundays...

That's why I don't think You want your events to be repeated MONTHLY. Wrong naming brings often to a wrong direction ;-)

Threre are a lot more scenarios, but they will need coding, date calculations etc. and my opinion is, You should first answer the "most important question" to Yourself :-)


It would be much easier to repeat every n weeks rather than n months.


I know that you can determine day of the week for any given date using Zeller's Congruence. Hope this helps.


Something I'm trying:

$dow = date('w',mktime(0, 0, 0, date("m",strtotime($date)), date("d",strtotime($date)), date("y",strtotime($date))));
$weeknum = date ("W",mktime(0, 0, 0, date("m",strtotime($date)), date("d",strtotime($date)), date("y",strtotime($date)))) -    date("W",strtotime(date("Y-m-01")));
$nextMonthDay = date ("m/d/y",strtotime("$weeknum week $dow", strtotime("2011-09-01")));

You'll need to replace $dow in the last line with Monday->Sunday, which can be done through an associate Array. You can then iterate into the future on the year/month values in the last line, and create rows in your table.


It's not very difficult. If you have a date, calculate which week it is (1-7 day is in first week, 8-14 is 2nd week, etc.)

Then add 28 days. Calculate again. If it falls in the same week, keep it. If not add 7 more days.


You should use PHP to put the days into an array.

  1. Find the number of days in the month: http://php.net/manual/en/function.cal-days-in-month.php

  2. Associate a day to each date using an array, you may also want to associate a date stamp to each instance.

    $nextmonth[7]['day']='Thursday';
    $nextmonth[8]['day']='Friday';
    ...
    
  3. Run through the array and search for the first, second, third of forth instance of the day according to the date specified by the user earlier. For example: 7 July 2011 is the first Thursday of July, find next month's first Thursday in the array.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜