我想检查php中两个日期之间是否存在sat或sun。
$fromDate = '03/21/2016';
$toDate = '03/28/2016';
我在这里尝试了其他解决方案,但他们并没有很好地运作。我希望它在laravel 5.2中,所以如果有任何简单的方法来处理这个,请指导我。谢谢!
更新 如果在两个日期之间有工作日(周一至周五),我也想知道。我需要它,因为用户只能选择sat和Sunday,所以在这种情况下,我可以隐藏他的工作日选项。所以我需要的是一种方法,告诉我开始和结束日期是周末还是工作日或两者都有。
答案 0 :(得分:2)
您可以使用date("N")
获取当天的当天日期,并添加日期之间的天数差异。如果这大于或等于6,则周末之间或周末之间是周末
//Get current day of week. For example Friday = 5
$day_of_week = date("N", strtotime($fromDate));
$days = $day_of_week + (strtotime($toDate) - strtotime($fromDate)) / (60*60*24)
//strtotime(...) - convert a string date to unixtimestamp in seconds.
//The difference between strtotime($toDate) - strtotime($fromDate) is the number of seconds between this 2 dates.
//We divide by (60*60*24) to know the number of days between this 2 dates (60 - seconds, 60 - minutes, 24 - hours)
//After that add the number of days between this 2 dates to day of week. So if first date is friday and days between this 2 dates is: 3 the sum will be 5+3 = 8 and it's bigger than 6 so we know it's a weekend between.
if($days >= 6){
//we have a weekend. Because day of week of first date + how many days between this 2 dates are greater or equal to 6 (6=Saturday)
} else {
//we don't have a weekend
}
答案 1 :(得分:2)
试
<?php
$is_weekend = 0;
$fromDate = strtotime('2016-03-21');
$toDate = strtotime('2016-03-28');
while (date("Y-m-d", $fromDate) != date("Y-m-d", $toDate)) {
$day = date("w", $fromDate);
if ($day == 0 || $day == 6) {
$is_weekend = 1;
}
$fromDate = strtotime(date("Y-m-d", $fromDate) . "+1 day");
}
echo $is_weekend;
锄头有帮助:)