PHP - If statement to show a special text on holidays

Wiola :

I am trying to find a way to generate a special text in PHP depending on which holiday it is. I was trying if-statement and switch but I cannot figure out the logic of it. Can you please advise how can it be done? The goal is to have two lines of text:

Today is Thursday.

Happy Fat Thursday!

If there is no holiday on a given day, only first line of text should be shown.

setlocale(LC_ALL, "sv_SE");
$dag = strftime('%A', time());
$datum = strftime('%D', time());

    {
    echo "Idag är det ".$dag."<br>";
    }

switch ($datum)
{
    case ($datum = '01/01/2020'):
        echo "Gott Nytt År";
    break;

    case ($datum = '01/06/2020'):
        echo "Glad Trettondedag Jul";
    break;        

    case ($datum = '02/20/2020'):
        echo "Happy Fat Thursday!";
    break;    

    default:
        echo ' ';
        break;
}
Mark Overton :

Try this

I'm not sure why you were using an expression in every case but you have already set the variable you are checking against as an argument to switch, so you just need to enter the values as the case.

I've also removed the first echo because I have no idea why that was there at all and I've moved it into the default catch, meaning that if no other case is triggered, the default will fire.

Also, leading on from @Cadu De Castro Alves comment, I changed $datum to:

$datum = strftime('%m/%d/%Y', time());

As the previous pattern you were using would return a two-digit year as opposed to the four-digits your checking for.

You can read more about switch here.


setlocale(LC_ALL, "sv_SE");
$dag = strftime('%A', time());
$datum = strftime('%m/%d/%Y', time());

switch ($datum) {
    case '01/01/2020':
        echo "Gott Nytt År";
    break;

    case '01/06/2020':
        echo "Glad Trettondedag Jul";
    break;        

    case '02/20/2020':
        echo "Happy Fat Thursday!";
    break;    

    default:
        echo "Idag är det ".$dag."<br>";
        break;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18282&siteId=1