Home > Mobile >  Echo conditional text based on day and time
Echo conditional text based on day and time

Time:04-21

I have some text I would like to echo based on the time of the day and day of the week.

On Monday it should say between 13.00 and 17.00: We zijn vandaag bereikbaar tot 17.00 uur.

On Tuesday till Friday it should say the same as above but then from 09.00 till 17.00.

Before 13.00 on Monday and 9.00 on Tuesday till Friday and after 17.00 and in weekends it should say: We zijn momenteel gesloten. Stuur een e-mail en we nemen z.s.m. contact met je op.

This is what I created to get a shortcode for it (Wordpress):

date_default_timezone_set('Europe/Amsterdam');

function customer_service_phone (){
    $dateOfWeek = date('w');
    $time = date('H');
    if ($dateOfWeek == '1' && $time > '13' && $time < '17') {
        $availability = "We zijn vandaag bereikbaar tot 17.00 uur.";
    } elseif ($dateOfWeek > '1' && $dateOfWeek < '6' && $time > '9' && $time < '17'){
        $availability = "We zijn vandaag bereikbaar tot 17.00 uur.";
    } else {
        $availability = "We zijn momenteel gesloten. Stuur een e-mail en we nemen z.s.m. contact met je op.";
    }
    echo esc_attr( $availability );
}
add_shortcode ( 'availability_phone', 'customer_service_phone' );

This code works not entirely good. Because after 17.00 it still says the text for before 17.00. What could it be? My servertime is correct with my timezone and also the time in Wordpress settings is the same. Timezone is Amsterdam.

Hope someone can help me with this. Any help would be appreciated. Sorry if I didn't explain it clear enough.

Kind regards, Vasco

CodePudding user response:

Try the following code.

function customer_service_phone (){
    $dateOfWeek = date('N');
    $time = date('H');
    if ($dateOfWeek == '1' && $time >= '13' && $time < '17') {
        $availability = "We zijn vandaag bereikbaar tot 17.00 uur.";
    } elseif ($dateOfWeek > '1' && $dateOfWeek < '6' && $time >= '09' && $time < '17'){
        $availability = "We zijn vandaag bereikbaar tot 17.00 uur.";
    } else {
        $availability = "We zijn momenteel gesloten. Stuur een e-mail en we nemen z.s.m. contact met je op.";
    }
    echo esc_attr( $availability );
}
add_shortcode ( 'availability_phone', 'customer_service_phone' );
  • Related