7

毎月の第 2 および第 4 土曜日を見つける方法。私はこれらの行を書きました:-

echo "may 2nd sat ".date('d', strtotime('may 2013 second saturday'));
echo '<br/>may 4th sat '.date('d', strtotime('may 2013 fourth saturday'));                                  
echo '<br/>june 2nd sat '.date('d', strtotime('june 2013 second saturday'));
echo '<br/>june 4th sat '.date('d', strtotime('june 2013 fourth saturday'));

次の出力が得られます:-

may 2nd sat 11
may 4th sat 25
june 2nd sat 15
june 4th sat 29

mayそれは月に正解を与えます がjune 2013jun 1013第 2 と第 4 土曜日にはそれぞれ 8 と 22 になるはずです。どうすればこの問題を解決できますか。

4

7 に答える 7

7

あなたのものがうまくいかない理由はわかりませんが、これを試してみてください。

echo '<br/>june 2nd sat '.date('d', strtotime('second sat of june 2013'));
echo '<br/>june 4th sat '.date('d', strtotime('fourth sat of june 2013'));

PHPマニュアルページ"first sat of July 2008"の例に基づいています

于 2013-06-11T06:53:17.850 に答える
1

私は通常、糸に頼ることはありません。カスタムメイドの機能はいかがですか?

function getSaturdayDay($year, $month, $position) {
    $firstDay = date('w', mktime(0, 0, 0, $month, 1, $year));
    $diff = 6 - $firstDay;

    return 1 + $diff + $position * 7;
}

そしてあなたの文脈でそれを使用してください

echo "may 2nd sat " . getSaturdayDay(2013, 5, 1);
echo '<br/>may 4th sat ' . getSaturdayDay(2013, 5, 3);                          
echo '<br/>june 2nd sat ' . getSaturdayDay(2013, 6, 1);
echo '<br/>june 4th sat ' . getSaturdayDay(2013, 6, 3);
于 2013-06-11T06:58:13.793 に答える
1

最近、私の好みの方法は、PHP のDateTime オブジェクトを拡張することです:-__

class MyDateTime extends DateTime
{
    /**
     * Returns a MyDateTime object set to 00:00 hours on the nth occurence
     * of a given day of the month
     *
     * @param string $n nth day required, eg first, second etc
     * @param string $day Name of day
     * @param mixed $month Month number or name optional defaults to current month
     * @param mixed $year optional defaults to current year
     *
     * @return MyDateTime set to last day of month
     */
    public function nthDayOfMonth($n, $day, $month = null, $year = null)
    {
        $timestr = "$n $day";
        if(!$month) $month = $this->format('M');
        $timestr .= " of $month $year";
        $this->setTimestamp(strtotime($timestr));
        $this->setTime(0, 0, 0);
        return $this;
    }
}
$dateTime = new MyDateTime();
echo $dateTime->nthDayOfMonth('second', 'Sun', 'Jul', 2011)->format('Y-m-d');

出力:-

2011-07-10
于 2013-06-11T06:54:55.400 に答える
0

毎年第 2 および第 3 土曜日を取得する

public function getSndFthSaturday()
    {
        $now = strtotime("01-01-2019");
        $end_date = strtotime("31-12-2019");
        $this->calculate($now, $end_date);
    }

    public function calculate($now, $end_date)
    {
        while (date("Y-m-d", $now) != date("Y-m-d", $end_date))
        {
            $day_index = date("w", $now);
            $day_indexD = floor((date("d", $now) - 1)/ 7);

            if ($day_index == 6 && ($day_indexD == 1 || $day_indexD == 3)) {
                $now1 = date("Y-m-d", $now);
                print_r($now1);
                echo "</br>";
            }
            $now = strtotime(date("Y-m-d", $now) . "+1 day");
        }
    }
于 2019-02-14T13:15:48.180 に答える