2011-02-02 9 views
0

旅行会社の予約システムを持っています。プランナーには、開始日にいくつかのオプションが用意されています。そのすべては、毎月の最初の土曜日にする必要があります。だから、PHPは今日の日付を取得する必要があります。PHPは、以降の月の特定の日を取得するために今日を使用します

次に、オプションボックス番号1の次のPHPは、次の月の最初の土曜日を表示するためにtodays日付を使用する必要があります。 2番目のボックスはそれ以降の月を示しています。

これを行う方法についての助けは素晴らしいことでしょう。

おかげ

+1

'date( 'Ym-d'、strtotime( '2011年2月1日の土曜日));' – Mchl

+0

エコーで何もしない –

+0

奇妙なことに、ここで「2011年2月5日」とエコーします。とにかく、 'strtotime()'を探索し、それが受け入れるフォーマットをします。 – Mchl

答えて

0

この例は非常に冗長であるが、それはPHP 5.3のDateTimeクラスとその関係の力を示す必要があります。

<?php 

$first_of_next_month = new DateTime('next month first day'); // get a start date 

$a_day = new DateInterval('P1D'); 
$a_month = new DateInterval('P1M'); 

$start_dates = array(); 

foreach (new DatePeriod($first_of_next_month, $a_month, 11) as $day_of_month) { 
    // $day_of_month is now the first day of the next month 

    while ($day_of_month->format('w') != 6) { 
    // while $day_of_month isn't a Saturday 
     $day_of_month->add($a_day); 
     // add a day 
    } 

    $start_dates[] = $day_of_month; 
} 

foreach ($start_dates as $d) { 
    echo $d->format('r'), "\n"; // or format how you like 
} 
+0

です。上にない必要があります5.3 –

+0

以下のコードのようなことはありませんか?明らかにそれはうまくいかないが、ここのすべての解決策は間違っている。<?php echo date( 'Y-m-d'、strtotime( '+ 2 month first saturday'));?> –

0
function next_ten_first_sat(){ 
    $next_month = strtotime(date('Y-m-d')); 
    $j=0; 
    for($i=1;$i<10;$i++){ 
     if($i==1){ 
      $sat_next_month[$i] = strtotime('next month first saturday', $next_month); 
      $next_month = strtotime(date('Y-m',$sat_next_month[$i]).'-01'); 
     }else{ 
      $sat_next_month[$i] = strtotime('next month first saturday', $next_month); 
      $next_month = strtotime(date('Y-m',$sat_next_month[$i]).'-01'); 
      $result_year = date('Y', $sat_next_month[$i]); 
      if(date('m', $sat_next_month[$i])>10){ 
       $result_month = date('m', $sat_next_month[$i])-1; 
      }else{ 
       $result_month = date('m', $sat_next_month[$i])-1; 
       $result_month = '0'.$result_month; 
      } 
      $result_date = date('d', $sat_next_month[$i]); 
      $result_array[$j] = $result_year.'-'.$result_month.'-'.$result_date; 
     }  
     $j++; 
    } 
    return $result_array; 
} 

申し訳ありませんが、みんな初めて質問を読み違えます。これは意図したとおりに動作するはずですが、非常に乱雑です...それらを最適化してください.....

関連する問題