如何在给定日期范围内的数组中存储工作日名称?

时间:2017-02-22 07:03:01

标签: php

我想在给定日期范围内的数组中存储工作日的名称。 例如。如果给定的日期范围是2016-12-1到2017-02-22那么我想存储所有工作日都在这个日期范围内。

3 个答案:

答案 0 :(得分:0)

$timestamp = strtotime('next Sunday');
$days = array();
for ($i = 0; $i < 7; $i++) {
$days[] = strftime('%A', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}

答案 1 :(得分:0)

试试这个。提供开始日期,结束日期,工作日作为输入参数

function progDateRange($date, $end_date, array $wkDays, array $excluded)
{
$dates = array();

$current_date = strtotime($date);
$max_date = min(strtotime('+2 years'), strtotime($end_date));
$dow = array_keys($wkDays);

while ($current_date < $max_date) {
    if ($excluded && in_array($date_formatted, $excluded, true)) {
        continue;
    }
    if (in_array(date('l'), $dow, true)) {
        array_push($dates, $date);
    }
    $current_date = strtotime('+1 day', $current_date);
    $date = date('Y-m-d', $current_date);
}

return $dates;
}

答案 2 :(得分:0)

使用datestrtotime函数的解决方案:

$date_from = '2016-12-1';
$date_to = '2017-02-22';
$current = strtotime($date_from);
$max_time = strtotime($date_to);
$weekdays = [];

while ($current <= $max_time) {
    // getting weekday name for current timestamp within date range
    $weekdays[] = date('l', $current);
    $current = strtotime('+1 day', $current);  // setting timestamp for each next next day in increase order
}

print_r($weekdays);

输出如下:

Array
(
    [0] => Thursday
    [1] => Friday
    [2] => Saturday
    [3] => Sunday
    [4] => Monday
    [5] => Tuesday
    [6] => Wednesday
    [7] => Thursday
    [8] => Friday
    [9] => Saturday
    [10] => Sunday
    [11] => Monday
    [12] => Tuesday
    [13] => Wednesday
    [14] => Thursday
    ....
    ...