Aki
Aki

Reputation: 85

Reset content every month

I currently have a code that resets every Saturdays, which the content is being displayed the entire week until next Saturday. I'd like to use the same approach for monthly basis (display the content for the entire month until next for a new content) and I can't get it to work. The weekly code is this:

$dayoftheweek = date("l");
$todayis = date('Y-m-d');

if($dayoftheweek == 'Saturday') { // The day it updates
    $dateUpdate = $todayis; 
} else {  
    $dateUpdate = date("Y-m-d", strtotime( "$todayis last Saturday" )); // The day it updates
}

// Content to display
switch($dateUpdate) {
    case '2020-05-02':
        // Content here
    break;
    case '2020-05-09':
        // Content here
    break;
    default:
        // Display content when ran out of rounds
}

I'm following the same code pattern for the monthly basis, am I doing something wrong for the strtotime or something? Here's the monthly code that I have right now:

$firstofmonth = date("d");
$thisday = date('Y-m-d');

if($firstofmonth == '01') {
    $MonthlyUpdate = $thisday;
} else {
    $MonthlyUpdate = date("Y-m-d", strtotime("first day of -1 month"));
}

The contents I'm supposed to have for monthly basis is not being displayed with the code above when I set the case date to '2020-05-01'. Again, am I doing something wrong for the strtotime or something? Or is there a better approach? Many thanks in advance!

Upvotes: 1

Views: 287

Answers (1)

dehart
dehart

Reputation: 1678

Leave the day (d) variable out and only check month + year:

switch(date('Y-m')) {
    case '2020-05':
        // Content here
    break;
    case '2020-06':
        // Content here
    break;
    default:
        // Display content when ran out of rounds
}

Upvotes: 1

Related Questions