简体   繁体   中英

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.

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.

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 MY",strtotime($date)); , if $date is already a date in format 'd MY' ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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