PHP: 根据当前工作日获取一周中接下来的几天,然后显示在图表标签中

PHP: Get next days of the week depending on current week day then display in chart label

我有一个显示网站每周访问量的图表,值的格式为 "d M Y",数据将从数据库中 return 编辑。

在当前状态下,仅显示我获得数据的日期的标签(例如,2016 年 8 月 1 日和今天)。

我也会 return 每个标签中接下来的几天(对于一周中的每一天),但请记住一周中的当前日期(例如,如果是星期三,则必须 return 2016 年 8 月 3 日,如果是星期四 return 2016 年 8 月 4 日等)相对于一周中的当前日期,那么如果是星期一(并且我有数据)returns 来自数据库的值,如果是星期二(今天)returns 数据库中的数据,但如果是星期三,则计算出我们得到了一周的第三天,因此将 +1 添加到当前日期,如果是星期四,则添加 + 2等 我希望我已经尽可能多地解释了我正在努力达到的结果

我试过 PHP 开关,但无法使用它达到结果,我认为我的逻辑完全错误。

function get_day($date,$daynum) {

$dayofweek = date("w",strtotime($date));

switch ($daynum) {
    case '1':
        if ($date == NULL) {
            $current = date('d M Y',strtotime("$date +1 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        }
        break;
    case '2':
        if ($date == NULL) {
            $current = date('d M Y',strtotime("$date +2 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        } 
        break;
    case '3':
        if ($date == NULL) {
            $current = date('d M Y',strtotime("$date +3 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        }
        break;
    case '4':
        if ($date == NULL) {
            $current = date('d M Y',strtotime("$date +4 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        }
        break;
    case '5':
        if ($date == NULL) {
            $current = date('d M Y',strtotime("$date +5 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        }
        break;
    case '6':
        if ($date == NULL) {
            $current = date('d M Y',strtotime("$date +6 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        }
        break;
    case '7':
        if ($date == NULL) {
            $current = date('d M Y', strtotime("$date +7 day"));
        } elseif ($date == date("d M Y")) {
            $current = 'Oggi';
        } else {
            $current = date("d M Y",strtotime($date));
        }
        break;
    }
return $current; 
}

我用

调用它
get_day($siteViewsThisWeek[0][1],'1');
get_day($siteViewsThisWeek[1][1],'2');
get_day($siteViewsThisWeek[2][1],'3');
[...]

感谢所有能提供帮助的人。

一开始就描述的毫无逻辑,怪不得代码难写。 我强烈建议为每种情况编写一些具有不同输入参数排列和预期结果的测试。

首先,if 可以帮助您下​​定决心,帮助我们了解您想要实现的目标,最后,确认它确实符合您的要求。

关于代码的几点说明:

你从不使用$dayofweek,不太清楚为什么要计算它。

接下来,构造如

if ($date == NULL) {
        $current = date('d M Y',strtotime("$date +1 day"));
}

strtotime 中使用 $date 没有什么意义,因为它在那里总是 null。 更别提这个计算的意义了。在哪种情况下 $date 在您的数据库中可以为空? (假设 $siteViewsThisWeek[0][1] 是 return 从数据库编辑的)

接下来,不太清楚,如果 $date 已经是 'd M Y' 格式的日期,您要用 $current = date("d M Y",strtotime($date)); 计算什么?

最后,switch-case 也没什么意义,因为你无缘无故重复代码 7 次。

equal 函数可以简单地写成:

function get_day($date,$daynum) {
    if (is_null($date)) {
        $current = date('d M Y',strtotime("+$daynum day"));
    } elseif ($date == date("d M Y")) {
        $current = 'Oggi';
    } else {
        $current = $date;
    }
    return $current; 
}

并且会 return 将来的某个日期,当 $date 为空时,'Oggi' 如果它匹配当前日期,否则就是日期本身。

我有意没有提到更严重的问题,例如在当前时间中继,使用 php 配置中的默认时区等。只有当您了解您要实现的目标时,这些问题才有意义。

this will surely help you
<?php
    $dt = new DateTime;
    if(isset($_GET['year']) && isset($_GET['week'])) {
    $dt->
    setISODate($_GET['year'], $_GET['week']);}
    else {
    $dt->setISODate($dt->format('o'), $dt->format('W'));
    }
    $year = $dt->format('o');
    $week = $dt->format('W');
    ?>
    <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.($week-  1) .   
    '&year='.$year; ?             >">Pre Week</a> 
    <a href="<?php echo $_SERVER['PHP_SELF'].'?week='.  
    ($week+1).'&year='.$year;  
    ?  >">Next Week</a>
    <table>
    <tr>
    <td>Employee</td>
    <?php
    do{
    echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "      
    </td>\n";
    $dt->modify('+1 day');
    }while ($week == $dt->format('W'));
    ?>