获取Php中不包括周末的两个日期范围之间的下拉列表

时间:2015-04-07 19:25:14

标签: php date date-range

我正在开发一个动态网站,其中网站后端管理员将选择两个日期(从 - 到),并且前端的用户将能够获得这两个日期之间的下拉列表,不包括周末以及名称这一天(例如:<option>27th April 2015 - Monday</option>

我尝试过这个链接,但徒劳无功: Link to get date range between two dates excluding weekends.

请帮助。

第一个日期的表格行名称为skype_date_start,第二个日期的表格行名称为skype_date_end

我的代码如下:

`$start = '2013/01/01'; //start date
$end = '2013/01/30'; //end date

$dates = array();
$start = $current = strtotime($start);
$end = strtotime($end);

while ($current <= $end) {
    $dates[] = date('Y/m/d', $current);
    $current = strtotime('+1 days', $current);
}

//now $dates hold an array of all the dates within that date range
print_r($dates);`

这只以群集形式打印数组,如: Array ( [0] => 2013/01/01 [1] => 2013/01/02 [2] => 2013/01/03 [3] => 2013/01/04 [4] => 2013/01/05 [5] => 2013/01/06 [6] => 2013/01/07 [7] => 2013/01/08 [8] => 2013/01/09 [9] => 2013/01/10 [10] => 2013/01/11 [11] => 2013/01/12 [12] => 2013/01/13 [13] => 2013/01/14 [14] => 2013/01/15 [15] => 2013/01/16 [16] => 2013/01/17 [17] => 2013/01/18 [18] => 2013/01/19 [19] => 2013/01/20 [20] => 2013/01/21 [21] => 2013/01/22 [22] => 2013/01/23 [23] => 2013/01/24 [24] => 2013/01/25 [25] => 2013/01/26 [26] => 2013/01/27 [27] => 2013/01/28 [28] => 2013/01/29 [29] => 2013/01/30 )

我无法排除周末,也无法将其打印为<option>27th April, 2015 - Monday</option><option>28th April 2015 - Tuesday</option>&amp;所以......到了最后的约会。

2 个答案:

答案 0 :(得分:0)

对于日期格式,请使用:

$dates[] = date('j F, Y - D', $current);

而不是

$dates[] = date('Y/m/d', $current);

跳过周末:(参考:https://stackoverflow.com/a/4802362/1741052

使用函数确定当前日期是否为周末

function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}

试试以下.. 代码示例:

$start = '2013/01/01'; //start date
$end = '2013/01/31'; //end date

$dates = array();
$start = $current = strtotime($start);
$end = strtotime($end);

while ($current <= $end) 
{
    $weekDay = date('w', $current); 
    print(" <br/> Week Day: {$weekDay}");
    if($weekDay != 0 && $weekDay != 6)
    {
        // Not a Weekend
        $dates[] = date('j F, Y - D', $current);    
        $current = strtotime('+1 days', $current);
    }
}

//now $dates hold an array of all the dates within that date range
print_r($dates);

答案 1 :(得分:0)

你快到了。在向dates数组添加日期之前,您需要检查它是否为工作日。如果是,则添加它否则跳过该日期并继续下一次迭代。像这样:

<?php
    $start = '2013/01/01'; //start date
    $end = '2013/01/30'; //end date

    $currentDate = strtotime($start);
    $endDate = strtotime($end);
    $dates = array();

    while ($currentDate <= $endDate ) {
        if(!isWeekend($currentDate)){
            $dates[] = date('Y/m/d', $currentDate );
        }

        $currentDate = strtotime('+1 days', $currentDate );
    }

    function isWeekend($date) {
        return date('w', $date) == 0 || date('w', $date) == 6;
    }
?>

接下来,您需要像这样创建select标记:

<select name=date_range=>
    <?php
         for($i = 0; $i < count($dates); $i++) {
              echo "<option value='{$dates[$i]}'>";
              echo date("Y/m/d - l", strtotime($dates[$i]));
              echo "</option>";
         }
    ?>
</select>
相关问题