获取两个日期范围之间的下拉列表,不包括 Php 中的周末
Get Dropdown List Between Two Date Range Excluding Weekends In Php
我正在开发一个动态网站,网站后端管理员将 select 两个日期(从 - 到),前端用户将能够获得这两个日期之间的下拉列表,不包括周末以及星期几(例如:<option>27th April 2015 - Monday</option>
)
我试过这个 link 但没有成功:
Link to get date range between two dates excluding weekends.
请帮忙。
第一个日期的 table 行名称是 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>
等等...直到最后一个日期。
对于日期格式使用此:
$dates[] = date('j F, Y - D', $current);
而不是
$dates[] = date('Y/m/d', $current);
跳过周末:(参考:)
使用函数确定当前日期是否是周末
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);
你快到了。在将日期添加到 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>
我正在开发一个动态网站,网站后端管理员将 select 两个日期(从 - 到),前端用户将能够获得这两个日期之间的下拉列表,不包括周末以及星期几(例如:<option>27th April 2015 - Monday</option>
)
我试过这个 link 但没有成功: Link to get date range between two dates excluding weekends.
请帮忙。
第一个日期的 table 行名称是 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>
等等...直到最后一个日期。
对于日期格式使用此:
$dates[] = date('j F, Y - D', $current);
而不是
$dates[] = date('Y/m/d', $current);
跳过周末:(参考:)
使用函数确定当前日期是否是周末
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);
你快到了。在将日期添加到 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>