2

月の平日にイベントを繰り返すことを許可したいカレンダーがあります。いくつかの例は次のとおりです。

  • 毎月第 4 火曜日を繰り返す
  • 毎月第 2 金曜日に繰り返す
  • 等々...

私が必要としているのは、今月これまでに何曜日 (たとえば火曜日) が経過したかを調べる機能です。

月曜日の経過回数を返すコードを見つけました。

$now=time() + 86400;
if (($dow = date('w', $now)) == 0) $dow = 7; 
$begin = $now - (86400 * ($dow-1));

echo "Mondays: ".ceil(date('d', $begin) / 7)."<br/>";

これはうまく機能しますが、曜日を特定できるようにするにはどうすればよいですか? この作業を行うためのコードに頭を悩ませているようには見えません。

4

2 に答える 2

1

strtotimeは、このような場合に非常に便利です。サポートされている構文のリストを次に示します。毎月第 2 金曜日に繰り返す例を使用して、次の簡単なスニペットを作成しました。

<?php
    $noOfMonthsFromNow=12;
    $dayCondition="Second Friday of";

    $months = array();
    $years = array();
    $currentMonth = (int)date('m');
    for($i = $currentMonth; $i < $currentMonth+$noOfMonthsFromNow; $i++) {
        $months[] = date('F', mktime(0, 0, 0, $i, 1));
        $years[] = date('Y', mktime(0, 0, 0, $i, 1));
    }
    for ($i=0;$i<count($months);$i++){
        $d = date_create($dayCondition.' '.$months[$i].' '.$years[$i]); 
        if($d instanceof DateTime) echo $d->format('l F d Y H:i:s').'<br>';
    }
?>

これはhttp://www.phpfiddle.org/lite/でテストできます。

于 2013-05-23T00:54:40.207 に答える
0
$beginningOfMonth = strtotime(date('Y-m-01')); // this will give you the timestamp of the beginning of the month
$numTuesdaysPassed = 0;
for ($i = 0; $i <= date('d'); $i ++) { // 'd' == current day of month might need to change to = from <= depending on your needs
    if (date('w', $beginningOfMonth + 3600 * $i) == 2) $numTuesdaysPassed ++; // 3600 being seconds in a day, 2 being tuesday from the 'w' (sunday == 0)
}

これが機能するかどうかはわかりませんが、おそらくもっと良い方法があります。今はテストする手段がありませんが、うまくいけば、これで正しい軌道に乗ることができます! (特にタイムゾーンでは、日付の計算にも少しつまずきます)

于 2013-05-23T00:54:24.233 に答える