在Php中获取两个日期范围(周末除外)之间的下拉列表
我正在开发一个动态网站,网站后端管理员将选择两个日期(从-到),前端用户将能够获得这两个日期之间的下拉列表,不包括周末以及当天的名称(例如:在Php中获取两个日期范围(周末除外)之间的下拉列表,php,date,date-range,Php,Date,Date Range,我正在开发一个动态网站,网站后端管理员将选择两个日期(从-到),前端用户将能够获得这两个日期之间的下拉列表,不包括周末以及当天的名称(例如:2015年4月27日-周一) 我试着通过这个链接,但没有成功: 请帮忙 第一个日期的表行名为skype\u date\u start,第二个日期的表行名为skype\u date\u end 我的代码如下: `$start = '2013/01/01'; //start date $end = '2013/01/30'; //end date $date
2015年4月27日-周一
)
我试着通过这个链接,但没有成功:
请帮忙
第一个日期的表行名为skype\u date\u start
,第二个日期的表行名为skype\u date\u 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);`
`$start='2013/01/01'//开始日期
$end='2013/01/30'//结束日期
$dates=array();
$start=$current=strottime($start);
$end=strottime($end);
而($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]=>2013/01/14[14]=>2013/01/15[2013/01/17]=>=>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[27]=>2013/01/28[28]=>2013/01/29[29]=>2013/01/30]
我不能排除周末,也不能将其打印为2015年4月27日至2015年4月28日星期一至星期二等等。。。到最后一个日期。对于日期格式,请使用以下命令:
$dates[]=日期('j F,Y-D',$current)代码>
而不是
$dates[]=日期('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);
$start='2013/01/01'//开始日期
$end='2013/01/31'//结束日期
$dates=array();
$start=$current=strottime($start);
$end=strottime($end);
而($current您就快到了。在将日期添加到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;
}
?>
我猜这个问题太难回答了!!好吧,首先,这里没有实际的问题。其次,这里也没有代码。你能把你当前的代码添加到这个问题中吗?你有什么问题吗?为什么它不能工作?它不能做什么?我是php的新手。你能帮我一个忙吗记下包括周末函数在内的全部完整代码?这将非常有帮助。weekend
函数已经存在。请看我的答案。我还为您提供了构建select
元素的代码。那么您还需要什么呢。它说isWeekend()的以下-缺少参数1
1970/01/01-Thursday1970/01/01-Thursday1970/01/01-Thursday1970/01/01-Thursday1970/01/01-Thursday1970/01/01-Thursday
我删除了一些不适合这里的选项。但是所有选项都是一样的。我忘记了在输出选项
标记之前将数据转换为时间戳。现在已经修复了。
<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>