duanliang789262 2017-07-19 09:19
浏览 43
已采纳

检查有关startdate的第5次重复。 在本月的特定工作日重复

I have got a script that repeats activitities. When the repeat settings are:

repeat "the 2nd tuesday every 2 months, stop at 5 instances" (like google calendar would do).

I can accomplish "the 2nd tuesday every 2 months" with the script below:

<?php
$pubDay = 19;
$pubMonth = 7;
$pubYear = 2017;
$repeatMonths = 2;

$newMonth = $pubMonth;
$newYear = $pubYear;

$raisedMonth = $pubMonth + $repeatMonths;
if ( $raisedMonth > 12 ) {
    $newMonth = ($raisedMonth) % 12; // off 12 at starts at 1
    $newYear = $pubYear + 1;
} else {
    $newMonth = $raisedMonth;
}

$occurenceInMonth = ceil($pubDay / 7); // determine the weekday occurence in the month (b.e. the "2nd thursday")
$dates = array();
foreach (getWeekDayDates($pubDow, $newYear, $newMonth) as $weekdaydate) {
    $dates[] = $weekdaydate->format("Y-m-d");
}
// we need the x occurence (-1)
$newPubDate = isset($dates[$occurenceInMonth -1]) ? $dates[$occurenceInMonth -1] . " " . $pubHour . ":" . $pubMin : "";

echo $newPubDate;

function getWeekDayDates($weekday, $y, $m) {
    return new DatePeriod(
        new DateTime("first " . $weekday . " of $y-$m"),
        DateInterval::createFromDateString('next ' . $weekday),
        new DateTime("next month $y-$m-01")
    );
}
?>

This works like a charm.

But now i need to check wether it is the 5th instance, starting at 17-9-2016. My script is now like this:

// get the end date
$startdate = "2016-09-17";
$repeatMonths = 2;
$endTime = strtotime($startdate . " +" . ($repeatMonths * $reps) . " months");
if ( $endTime >= strtotime($newPubDate) ) {
    $doRepeat = true;
}

But this can go wrong!

By example when the repetitons starts (startddate) at saturday 4-7 and it repeats every first sunday. When the sunday in the last repetition is on the 6th of the month. The script above returns false, but it shouldnt't.

How can i check on a simple way if it is the 5th occurence?

  • 写回答

1条回答 默认 最新

  • douchang6770 2017-07-19 13:19
    关注

    I would create an array which holds DateTime objects of the next 5 "2nd tuesdays of the month":

    $startdate = new \DateTime('second tue of february 2017');
    
    $dates = array();
    $repetitions = 5;
    for ($i=0; $i<$repetitions; $i++) {
        $dates[] = clone $date->modify('+1 month');
    }
    

    Using this array it should be easy to check, whether the date is reached or not yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测