在特定日期的每月重复活动,如果天数少于用户选择的日期,则跳过任何月份 [英] Monthly recurring event on specific date and skip any month if have less no of days compare to user's selected date

查看:51
本文介绍了在特定日期的每月重复活动,如果天数少于用户选择的日期,则跳过任何月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,用户可以创建每月活动(就像Google/Outlook),因此,如果用户选择了任何月份的第31个日期,而下个月则选择了 30天code>接下来的所有日期都将更改为 30th .

I am creating a app where user can create monthly event (Just like Google/Outlook), so if user has selected 31st date of any month and next month have 30 days than all next dates are changing to 30th.

30th 相同,可以说用户选择的 12月30日比2月之后的所有下一个日期更改为 28th .

Same goes with 30th, lets say user selected 30th of December than after Feb all next date is changing to 28th.

因此,如果下个月的日期少于用户选择的日期,则将日期更改为该日期.

so if in next month days are lesser than user's selected date its changing the date to that.

示例:开始日期: 2020-10-31 结束日期: 2021-11-30

Example : Start Date : 2020-10-31 End Date : 2021-11-30

$diffInMonths = $startDate->diffInMonths($endDate);
for ($i = 0; $i <= $diffInMonths; $i++) {
    $newStartDate = $i == 0 ? $startDate : $startDate->addMonthWithNoOverflow();
    print('<pre>' . print_r($newStartDate->toDateString(), true) . '</pre>');
}

它给出这样的输出

2020-10-31
2020-11-30
2020-12-30
2021-01-30
2021-02-28
2021-03-28
2021-04-28
2021-05-28
2021-06-28
2021-07-28
2021-08-28
2021-09-28
2021-10-28

如果该月中的天数少于用户选择的日期,则我需要跳过该月.在上述示例中,正确的输出应为

What i need is skip the month if in that month have less days than user;s selected date. With Above example the correct output should be

2020-10-31 //November has less than 31st day
2020-12-31
2021-01-31 //Feb has less than 31st day
2021-03-31 //April has less than 31st day
2021-05-31 //June & July has less than 31st day
2021-07-31 
2021-08-31 //September has less than 31st day
2021-10-31

过去2-3天在此方面苦苦挣扎,因此,任何一种帮助或指导都将使我的生活变得美好:)

Struggling on this from last 2-3 days so any Kind of help or guidance will made my day :)

推荐答案

这可能是愚蠢的方式,但是由于时间有限,这里是解决方法

This might be dumb way of doing this, but as i am running out of time so here is solution

$dateRange = Carbon::parse($callPlanner->start_date)->toPeriod($callPlanner->end_date, 1, 'month');
        $startDate = Carbon::parse($callPlanner->start_date);

        foreach ($dateRange as $date) {
            // compare the event date with end of month date
            if ($startDate->day <= $date->endOfMonth()->day) {
                //create new date with this month's year, month and event start date
                $newDate = Carbon::createFromDate($date->endOfMonth()->year, $date->endOfMonth()->month, $startDate->day);
                echo $newDate->toDateString(). "\n";
            }
        }

这篇关于在特定日期的每月重复活动,如果天数少于用户选择的日期,则跳过任何月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆