dsx666666 2016-08-02 20:02
浏览 58
已采纳

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

I have a chart that displays weekly visits to the site, the values are in the format "d M Y" and data will be returned from the database.

At current state only display labels for days which I got data (eg. August 1, 2016 and today ).

I would also return the next days in each label (for each day of the week), but keep in mind the current day of the week (eg. if is Wednesday must return August 3, 2016, if is Thursday return August 4, 2016 etc.) relative to the current day of the week, so if it's Monday (and I have data) returns the value from the database, if it's Tuesday (today) returns data from the database, but if it's Wednesday it is calculated that We got Third day of the week so adds +1 to the current date, if it will be on Thursday adds +2 etc. I hope I have explained much as possible as a result I'm trying to reach

I've tried to make a PHP switch but can't reach a result using this, and I'm thinking my logic is totally wrong.

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; 
}

I'm calling this using

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

Thanks to all who can help.

  • 写回答

2条回答 默认 最新

  • duanci1858 2016-08-02 21:52
    关注

    There is no logic in the description at the first place, so no wonder it is hard to write the code. I strongly recommend to write some tests with different permutations of input parameters and expected results for each case.

    First of all if may help you make your mind up, help us to understand what you're trying to achieve, and finally, confirm it actually does what you want.

    A few notes about the code:

    You never use $dayofweek, and it is not quite clear why you calculate it.

    Next, construction like,

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

    makes little sense to use $date in strtotime, as it always null there. Not mentioning the meaning of this calculation. In which case $date can be null in your database? (Assuming $siteViewsThisWeek[0][1] is returned from database)

    Next, it is not quite clear, what you are trying to calculate with $current = date("d M Y",strtotime($date));, if $date is already a date in format 'd M Y' ?

    Finally, there is no much sense in switch-case either, as you repeat the code 7 times with no reason.

    The equal function can be written as simple as:

    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; 
    }
    

    and will return some date in the future when $date is empty, 'Oggi' if it matches current date, and date itself otherwise.

    I intentionally did not mention more serious problems, like relaying on current time, using default timezone from php config, etc. It all matters only when you understand what you are trying to achieve.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作