2011-02-02 116 views
0

爲旅遊公司預訂系統。它給計劃者在開始日期的幾個選項,所有這些都應該是每個月的第一個星期六。所以PHP需要得到今天的日期。PHP使用今天的日期獲取後幾個月的具體日期

然後,選項框編號1中的以下PHP應使用今天的日期來顯示下個月的第一個星期六。第二個盒子顯示之後的月份。等等..

任何關於如何做到這一點的幫助將是奇妙的。

感謝

+1

`date('Ym-d',strtotime('2011年2月第一個星期六'));` – Mchl 2011-02-02 13:08:23

+0

似乎回聲 – 2011-02-02 13:16:51

回答

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
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; 
} 

對不起球員第一次誤讀的問題。這應該按預期工作,但它非常混亂...請​​優化它的傢伙.....

相關問題