andreaem andreaem - 4 months ago 7
PHP Question

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 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.

Answer

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.

Comments