user3360972
user3360972

Reputation: 107

PHP check certain date and give output

I have a php script that checks which day of the week it is and then gives the opening hours for that day. (this part works) But I want to include holidays. For now I'm trying if the holiday is on 5/5/2016.

For some reason it's failing..

Here is my code:

PHP - edit

<?php
    $strVerlofDag = '05/05/2016';
    if (date('d/m/Y') == $strVerlofDag);
    {
        echo 'Gesloten wegens O.H. Hemelvaart';
    }
    else
    {
        $vandaag = date("l");
        switch($vandaag)
        {
            case 'Monday':
                echo 'Maandag: 14u - 19u';
                break;
            case 'Tuesday':
                echo 'Dinsdag: 14u - 19u';
                break;
            case 'Wednesday':
                echo 'Woensdag: : 14u - 19u';
                break;
            case 'Thursday':
                echo 'Donderdag: 14u - 19u';
                break;
            case 'Friday':
                echo 'Vrijdag: : 14u - 19u';
                break;
            case 'Saturday':
                echo 'Zaterdag: 9u - 17u';
                break;
            case 'Sunday':  
                echo 'Zondag: gesloten';
                break;  
        }
    }
?>

Upvotes: 0

Views: 58

Answers (3)

cmason
cmason

Reputation: 51

It's usually best to keep to KISS (keep it simple stupid) design whenever possible. Why convert date formats instead of just getting the current date in the format you want?

$strHolidayDate = '05/05/2016';
if (date('d/m/Y') == $strHolidayDate);
{
    echo 'Closed for holiday';
}
else
{
    //... do switch logic here.
}

This would also let you convert the code to using an array of holiday dates (such something you're retrieve from a DB) very easily.

There is also no need to have a 'break' in your first if statement, outside of the switch.

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 98921

The checkdate funtion takes months and days without the leading zero (int)

bool checkdate ( int $month , int $day , int $year )

Valid examples:

checkdate(2, 29, 2016);
checkdate(9, 8, 2016);

Here's another way to achieve what you want:

<?php
date_default_timezone_set( "Europe/Lisbon" );
$holliday = new DateTime( "04/25/2016" );
$holliday->format( "m/d/Y" );
$today = new DateTime( "today" );

if( $holliday == $today ){
   echo "Holliday";
}

Live Example

Upvotes: 0

Jeff Puckett
Jeff Puckett

Reputation: 40891

checkdate always return true if it's a valid date, including all holidays (and non-holidays), so that's why your logic won't work as written.

I would think you'll need to hard code an array of your specific holiday dates, and then use something like in_array to see if it's in there.

Upvotes: 1

Related Questions