php check if the time is now between 09:00 and 12:00 on a sunday

I´m trying to make a php code that checks if it´s sunday morning or not sunday morning anymore (between 09:00 and 12:00)

It´s supposed to work 100% but I can´t see what I´m doing wrong

<?php

    date_default_timezone_set('Europe/Stockholm'); // timezone 

    $date = strtotime(time());

    if ((date('w', $date) == 0 && date('H', $date) >= 09) ||
    (date('w', $date) == 0 && date('H', $date) <= 12)) {
    echo "yes, it´s sunday morning";
    } else {
    echo 'nope, not sunday morning anymore';
    }

?>

Note: It´s just any given sunday - no firm date Not sure if the timezone script is the issue

Right now it says "nope, not sunday morning" wheather I change the value from 12 to 18 or not

Upvotes: 1

Views: 213

Answers (3)

ron
ron

Reputation: 826

try this:

<?php
date_default_timezone_set('Europe/Stockholm');
$current_day = date("w");//get the current day : 0 is sunday
$time_now = date("H:i:s");
echo $current_day;
echo($time_now);
if($current_day == 0){//today it is sunday
    if($time_now >= "09:00:00" && $time_now <="12:00:00"){
         echo "yes, it´s sunday morning";
    }
    else{
         echo "yes, it´s sunday but not morning";
    }
}
else{
    echo 'nope, not sunday';
}

?>

Upvotes: 1

Archemar
Archemar

Reputation: 571

Try:

<?php

    date_default_timezone_set('Europe/Stockholm'); // timezone 

    $date = strtotime(time());

    if ( date('w', $date) == 0 
      && date('H', $date) >= 09 
      && date('H', $date) <= 12 ) {
    echo "yes, it´s sunday morning";
    } else {
    echo 'nope, not sunday morning anymore';
    }

?>

Your code was wrong, every time in sunday is either after 9 (date('w', $date) == 0 && date('H', $date) >= 09) or before 12 (date('w', $date) == 0 && date('H', $date) <= 12) .

Upvotes: 0

dotancohen
dotancohen

Reputation: 31471

The given code just checks if the day is Sunday. This will check for Sunday morning:

<?php

    date_default_timezone_set('Europe/Stockholm'); // timezone 

    $date = strtotime(time());

    if ((date('w', $date) == 0 && date('H', $date) >= 09) && date('H', $date) <= 12)) {
        echo "yes, it´s sunday morning";
    } else {
        echo 'nope, not sunday morning anymore';
    }

?>

Note the && instead of ||.

Upvotes: 0

Related Questions