Repeating Events on the “nth” Weekday of Every Month

拥有回忆 提交于 2019-11-30 05:21:59

Here's a possible alternative for you. strtotime is powerful, and the syntax includes really useful bits like comprehensive relative time and date wording.

You can use it to generate the first through Nth specific weekdays of a specific month by using the format " of ". Here's an example using date_create to invoke a DateTime object, but regular old strtotime works the same way:

php > $d = date_create('Last Friday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Friday March 25 2011 00:00:00
php > $d = date_create('First Friday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Friday March 04 2011 00:00:00
php > $d = date_create('First Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday March 06 2011 00:00:00
php > $d = date_create('Fourth Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday March 27 2011 00:00:00
php > $d = date_create('Last Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday March 27 2011 00:00:00

It will also overflow the month, if you ask for an invalid one:

php > $d = date_create('Ninth Sunday of March 2011'); if($d instanceof DateTime) echo $d->format('l F d Y H:i:s');
Sunday May 01 2011 00:00:00

Note that it only works with the ordinal number wording. You can't pass "1st" or "3rd", unfortunately.

Once you use this to grab the proper Nth weekday, you can then simply add the number of needed weekdays to skip (7 for one week, 14 for two, 21 for three, etc) as required until the designated end date or designated number of weeks has passed. Once again, strtotime to the rescue, using the second argument to chain together relativeness:

php > echo date('l F d Y H:i:s', strtotime('+14 days', strtotime('First Thursday of March 2011')));
Thursday March 17 2011 00:00:00

(My local copy also accepted '+14 days First Thursday of March 2011', but that felt kind of weird.)

I have a more simple method, although it may seem hack-ish.

What we can do is take the start date, and infer it's "n" value from it by taking the ceiling of its day divided by 7.

For example, if your start date is the 18th, we divide by 7 and take the ceiling to infer that it is the 3rd "whatever-day" of the month.

From there we can simply repeatedly add 7 days to this to get a new date, and whenever the new date's ceilinged 7-quotient is 3, we've hit the next "nth whatever-day" of the month.

Some code can be seen below:

$day_nth = ceil(date("j", strtotime($date))/7);
for ($i = 0; $i < $number_of_repeats; $i++) {

    while (ceil(date("j", strtotime($date))/7) != $day_nth)
        $date = date("Y-m-d", strtotime("+7 days", strtotime($date)));

    /* Add your event, or whatever */

    /* Now we increment date by 7 days before the loop starts again,
    ** otherwise the while loop would never execute and we'd just be
    ** adding to the same date repeatedly. */
    $date = date("Y-m-d", strtotime("+7 days", strtotime($date)));
}

What's nice about this method is that we don't even have to concern ourselves with which day of the week we're dealing with. We just infer everything from the start date and away we go.

I make it like that:

            //$typeOfDate = 0;
            $typeOfDate = 1;
            $day = strtotime("2013-02-01");
            $to = strtotime(date("Y-m-d", $day) . " +6 month");

            if ($typeOfDate == 0) { //use the same day (number) of the month
                $dateArray[] = date("Y-m-d", $day);
                $event_day_number = date("d", $day);
                while ($day <= $to) {
                    $nextMonth = date("m", get_x_months_to_the_future($day));
                    $day = strtotime(date("Y-" . $nextMonth . "-d", $day));
                    $dateArray[] = date("Y-m-d", $day);
                }
            }

            if ($typeOfDate == 0) { //use the same day (like friday) of the month
                $dateArray[] = date("Y-m-d", $day);
                $monthEvent = date("m", $day);
                $day = strtotime(date("Y-m-d", $day) . " +4 weeks");
                $newMonthEvent = date("m", $day);
                if ($monthEvent == $newMonthEvent) {
                    $day = strtotime(date("Y-m-d", $day) . " +7 days");
                }
                while ($day <= $to) {
                    $dateArray[] = date("Y-m-d", $day);
                    $monthEvent = date("m", $day);
                    $day = strtotime(date("Y-m-d", $day) . " +4 weeks");
                    $newMonthEvent = date("m", $day);
                    if ($monthEvent == $newMonthEvent) {
                        $day = strtotime(date("Y-m-d", $day) . " +7 days");
                    }
                }
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!