4

私は問題があります、

$fridays = array();
$fridays[0] = date('Y-m-d', strtotime('first friday of this month'));
$fridays[1] = date('Y-m-d', strtotime('second friday of this month'));
$fridays[2] = date('Y-m-d', strtotime('third friday of this month'));
$fridays[3] = date('Y-m-d', strtotime('fourth friday of this month'));
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));

しかし、第 5 金曜日はありません。一部の月には第 5 金曜日があります。最後の項目配列を確認して設定しない方法は?

4

8 に答える 8

7
$fifth = strtotime('fifth friday of this month');

if (date('m') === date('m', $fifth)) {
  $fridays[4] = date('Y-m-d', $fifth);
}
于 2012-09-24T08:51:54.400 に答える
1

私は自分のアプリケーションに月の金曜日を数える機能を持っています....誰かにとって役立つかもしれません。

function countFridays($month,$year){
	$ts=strtotime('first friday of '.$year.'-'.$month.'-01');
	$ls=strtotime('last day of '.$year.'-'.$month.'-01');
	$fridays=array(date('Y-m-d', $ts));
	while(($ts=strtotime('+1 week', $ts))<=$ls){
		$fridays[]=date('Y-m-d', $ts);
	}return $fridays;
}

于 2016-03-06T04:04:58.503 に答える
1

これは、PHP の日付関数を使用して行うことができます。必要な月を取得してから、次の$timestampようにします。

<?php
function fridays_get($month, $stop_if_today = true) {

$timestamp_now = time();

for($a = 1; $a < 32; $a++) {

    $day = strlen($a) == 1 ? "0".$a : $a;
    $timestamp = strtotime($month . "-$day");
    $day_code = date("w", $timestamp);
    if($timestamp > $timestamp_now)
        break;
    if($day_code == 5)
        @$fridays++;

}

return $fridays;
}

echo fridays_get('2011-02');

これに関する同様の投稿を見つけることができます: In PHP, how to know how many mondays has changed in this month uptil today?

于 2012-09-24T08:55:48.753 に答える
0

月に 5 つの金曜日があるのは、30 日あり、最初の金曜日がその月の 1 日または 2 日である場合、または 31 日あり、最初の金曜日がその月の 1 日、2 日、または 3 日である場合のみです。したがって、条件ステートメントを作成できます。第5金曜日のこの計算に基づいて

于 2012-09-24T08:51:09.910 に答える
0

私はこれをテストできるマシンを使用していませんが、次のようなものはどうでしょう....

if(date('Y-m-d', strtotime('fifth friday of this month')) > ""){
$fridays[4] = date('Y-m-d', strtotime('fifth friday of this month'));
}

以下のリンクはまったく同じことを行い、上記のような不格好な if ステートメントなしでやりたいことを正確にカバーします..

非常に似た読み方: 続きを読む...

于 2012-09-24T08:49:56.737 に答える
0
for($i=0;$i<=5;$i++)
{
       echo date("d/m/y", strtotime('+'.$i.' week friday september 2012'));
}
于 2012-09-24T09:49:51.740 に答える