删除php strtotime()中的周末

时间:2016-09-15 16:42:32

标签: php html strtotime

我从API中获取数据,其中包含几周内的一些API数据并在HTML表格中获取数据,我在一列中每天获取数据,星期三在一列中,星期四在1列中,...到下周三。

它显示了星期六和星期日,但我想删除它们。以下是我的代码:

$fromDate = (isset($_POST['fromDate'])) ? $_POST['fromDate'] : date("Y-m-d");
$toDate = date("Y-m-d", strtotime("+1 week", strtotime($fromDate)));

我认为需要在(+1周)行编辑某些内容,但我无法做到。

1 个答案:

答案 0 :(得分:0)

此代码段将在接下来一周的同一天内迭代共8天($fromDate,并且仅保留工作日。

$fromDate=date("Y-m-d");
for($x=0; $x<8; ++$x){
    $date=date("Y-m-d",strtotime("$fromDate +$x day"));
    echo "<br>Date: $date is a ",date('l',strtotime($date))," represented by number ",date('N',strtotime($date));
    if(date('N',strtotime($date))<6){
        echo " WEEKDAY";
        $result[]=$date;
    }else{
        echo " WEEKEND";
    }
}
echo "<pre>";
var_export($result);
echo "</pre>";

从星期二开始输出:

Date: 2017-03-14 is a Tuesday represented by number 2 WEEKDAY
Date: 2017-03-15 is a Wednesday represented by number 3 WEEKDAY
Date: 2017-03-16 is a Thursday represented by number 4 WEEKDAY
Date: 2017-03-17 is a Friday represented by number 5 WEEKDAY
Date: 2017-03-18 is a Saturday represented by number 6 WEEKEND
Date: 2017-03-19 is a Sunday represented by number 7 WEEKEND
Date: 2017-03-20 is a Monday represented by number 1 WEEKDAY
Date: 2017-03-21 is a Tuesday represented by number 2 WEEKDAY
array (
  0 => '2017-03-14',
  1 => '2017-03-15',
  2 => '2017-03-16',
  3 => '2017-03-17',
  4 => '2017-03-20',
  5 => '2017-03-21',
)

从星期六开始输出:

Date: 2017-03-11 is a Saturday represented by number 6 WEEKEND
Date: 2017-03-12 is a Sunday represented by number 7 WEEKEND
Date: 2017-03-13 is a Monday represented by number 1 WEEKDAY
Date: 2017-03-14 is a Tuesday represented by number 2 WEEKDAY
Date: 2017-03-15 is a Wednesday represented by number 3 WEEKDAY
Date: 2017-03-16 is a Thursday represented by number 4 WEEKDAY
Date: 2017-03-17 is a Friday represented by number 5 WEEKDAY
Date: 2017-03-18 is a Saturday represented by number 6 WEEKEND
array (
  0 => '2017-03-13',
  1 => '2017-03-14',
  2 => '2017-03-15',
  3 => '2017-03-16',
  4 => '2017-03-17',
)