简体   繁体   English

PHP:根据当前工作日获取星期几,然后显示在图表标签中

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

I have a chart that displays weekly visits to the site, the values are in the format "d MY" and data will be returned from the database. 我有一个图表,显示每周对该站点的访问,这些值的格式为“ d MY”,并且将从数据库中返回数据。

At current state only display labels for days which I got data (eg. August 1, 2016 and today ). 在当前状态下,仅显示我有数据的日期的标签(例如,2016年8月1日和今天)。

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 我还会在每个标签中返回星期几(对于一周中的每一天),但请记住星期几(例如,如果星期三返回2016年8月3日,如果星期四返回2016年8月4日,等等)相对于星期几,因此,如果是星期一(并且我有数据)从数据库中返回值,那么如果是星期二(今天)则从数据库中返回数据,但是如果是星期三,则计算得出一周的第三天,所以要在当前日期前加上+1,如果是星期四,则要加上+2,依此类推。我希望我已尽可能多地说明了这一点

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. 我试图进行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; 
}

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. 感谢所有能提供帮助的人。

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. 您永远不会使用$dayofweek ,并且不清楚为什么要计算它。

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. strtotime使用$date几乎没有意义,因为它总是在那里为null。 Not mentioning the meaning of this calculation. 没有提及此计算的含义。 In which case $date can be null in your database? 在哪种情况下$date在您的数据库中可以为null? (Assuming $siteViewsThisWeek[0][1] is returned from database) (假设从数据库返回$siteViewsThisWeek[0][1]

Next, it is not quite clear, what you are trying to calculate with $current = date("d MY",strtotime($date)); 接下来,还不清楚,您要使用$current = date("d MY",strtotime($date));来计算什么? , if $date is already a date in format 'd MY' ? ,如果$date已经是'd MY'格式的日期?

Finally, there is no much sense in switch-case either, as you repeat the code 7 times with no reason. 最后,切换情况也没有太大意义,因为您无缘无故地重复执行了7次代码。

The equal function can be written as simple as: 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; 
}

and will return some date in the future when $date is empty, 'Oggi' if it matches current date, and date itself otherwise. 并在$ date为空时返回将来的某个日期,如果与当前日期匹配则返回'Oggi',否则返回日期本身。

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. 我故意没有提到更严重的问题,例如在当前时间进行中继,使用php config中的默认时区等。所有这些仅在您了解要实现的目标时才重要。

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'));
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM