2011-02-02 121 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; 
} 

对不起球员第一次误读的问题。这应该按预期工作,但它非常混乱...请​​优化它的家伙.....

相关问题