如何从一个开始日期填充几周并结束到一个下拉列表?

时间:2016-03-27 10:09:55

标签: javascript php jquery html drop-down-menu

如何从startdate填充周数并结束到下拉列表? 假设我希望在这个月内获得所有周。但是,如何将它们填入下拉周数?

到目前为止,我已经尝试过了:

<?php
$start = "2016-03-01";
$end = "2016-03-31";    
$first_day = $start.' first Sunday of this month';
$first_day = date_create($first_day);
$first_day = $first_day->format('Y-m-d');
$last_day = $start.' last Saturday of this month';
$last_day = date_create($last_day);
$last_day = $last_day->format('Y-m-d');

if($start != $first_day) {
$date_str = $start.' last Sunday of last month';
$start_date = date_create($date_str);
$start_date = $start_date->format('Y-m-d');
}
else {
$start_date = $start;    
}

if($end != $last_day) {
$date_str = $end.' first Saturday of next month';
$end_date = date_create($date_str);
$end_date= $end_date->format('Y-m-d');
}
else {
$end_date = $end;    
}
?>

//result
//start_date = "2016-02-28";
//end_date= "2016-04-02";

以上代码将为我提供一个月内完整周数的开始日期和结束日期,其中星期日为第一天,星期六为一周的最后一天。但是,我想根据开始日期和结束日期将这几周填入下拉列表中。怎么做?

我想将以下值输出到下拉列表中:

<select>
<option value="2016-02-28 to 2016-03-05">Week 1</option>
<option value="2016-03-06 to 2016-03-15">Week 2</option>
<option value="2016-03-13 to 2016-03-19">Week 3</option>
<option value="2016-03-20 to 2016-03-26">Week 4</option>
<option value="2016-03-27 to 2016-04-02">Week 5</option>
</select>

请告知如何使用jQuery或PHP将上面的输出填充到下拉列表中。

1 个答案:

答案 0 :(得分:2)

试试这个:

<select>
<?php 
   $start_month = "2016-03-01";
   $end_month   = "2016-03-31";  
   while (strtotime($start_month) <= strtotime($end_month)) {
      $week_start  =  date('Y-m-d',strtotime($start_month.' last Sunday'));
      $week_end    =  date('Y-m-d',strtotime($start_month.' Saturday this week'));
?>

<option value='<?php echo $week_start." to ".$week_end; ?>' >
    <?php echo $week_start." to ".$week_end; ?>
</option>       

<?php   $start_month = date("Y-m-d", strtotime($start_month." +7 days"));

} ?>
</select>

希望这有帮助。