我正在尝试创建接下来5个工作周的数组(周一至周五,今天除外)。我知道世界各地的工作周各不相同,但这对我要做的事情并不重要。
所以,例如,如果今天是星期三,我想要当前星期四和星期五以及下一周的星期一,星期二和星期三的日期。
我认为这样可行:
$dates = array();
for ($i = 1; $ < 6; $i ++)
{
$dates[] = date('Y-m-d', strtotime('+ '.$i.' weekday'));
}
但是今天它给了我:
任何建议表示赞赏。
由于
答案 0 :(得分:7)
试试这个
$dates = array();
$date = new DateTime();
while (count($dates)<5)
{
$date->add(new DateInterval('P1D'));
if ($date->format('N')<6)
$dates[]=$date->format('Y-m-d');
}
答案 1 :(得分:1)
function getWeekdayDatesFrom($format, $start_date_epoch, $end_date_epoch, $range) {
$dates_arr = array();
if( ! $range) {
$range = round(abs($start_date_epoch-$end_date_epoch)/86400) + 1;
} else {
$range = $range + 1; //end date inclusive
}
$current_date_epoch = $start_date_epoch;
for($i = 1; $i <= $range; $i+1) {
$d = date('N', $current_date_epoch);
if($d <= 5) { // not sat or sun
$dates_arr[] = "'".date($format, $current_date_epoch)."'";
}
$next_day_epoch = strtotime('+'.$i.'day', $start_date_epoch);
$i++;
$current_date_epoch = $next_day_epoch;
}
return $dates_arr;
}
答案 2 :(得分:0)
这应该有效:
$dates = array();
$i = 0;
while(true) {
$i++;
$time = strtotime("+$i days");
$dayOfWeek = date('w', $time);
/* 'w' - day of week, numeric, i.e. "0" (Sunday) to "6" (Saturday) */
if( ($dayOfWeek == 0) or ($dayOfWeek == 6) ) {
continue;
}
$dates[] = date('Y-m-d', $time);
if( count($dates) >= 5 ) {
break;
}
}
答案 3 :(得分:0)
$now = time();
$day = 60*60*24;
$days = array();
for ($i=1; $i<=7; $i++){
$ts = $now + $day*$i;;
if (date("N", $ts) < 6){
$days[] = date("l jS", $ts);
}
}
输出结果为:
Array(
[0] => Monday 1st
[1] => Tuesday 2nd
[2] => Wednesday 3rd
[3] => Thursday 4th
[4] => Friday 5th
)