2017-07-19 7 views
0

私は活動を繰り返すスクリプトを持っています。 リピート設定がある場合:開始日に関する5回目の再発を確認します。月の特定の平日に繰り返される

繰り返し「第二火曜日ごとに2ヶ月、5つのインスタンスで停止」(Googleカレンダーはどうなるように)。

私は以下のスクリプトで「2ヶ月ごとに第二火曜日」を達成することができます:これは魔法のように動作

<?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") 
    ); 
} 
?> 

今、私はそれが5番目のインスタンスであることを確認する必要があります.17-9-2016から開始します。

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

しかし、これは間違って行くことができます。 私のスクリプトは次のようになりました!

リピートンが土曜日4-7日に開始され(startddate)、最初の日曜日が繰り返される例です。 最後の繰り返しの日曜日が6日にある場合。上のスクリプトはfalseを返すが、それはすべきではない。

5回目の場合はどうすれば簡単に確認できますか?

答えて

1

私は次の5「毎月第2火曜日」のDateTimeオブジェクトを保持する配列作成します。日になるかはまだされているかどうか、チェックが容易であるべきで、この配列を使用して

$startdate = new \DateTime('second tue of february 2017'); 

$dates = array(); 
$repetitions = 5; 
for ($i=0; $i<$repetitions; $i++) { 
    $dates[] = clone $date->modify('+1 month'); 
} 

を。

+0

私はそれもやっていると私はそれを試してみました。だから、この解決策が働いています。私と一緒に考えてくれてありがとう。それは私が今正しい方法を考えていることを確認する。 –

関連する問題