marconalida
marconalida

Reputation: 51

Day of the week between two other days

I got two dates stored with date("w") in PHP (day of the week, from 0 to 6), and I need to know if current day (again with w) is between those two. Pretty easy, except for the sunday part. I can't just do something like

$now = date("w");
if ($first_day < $now < $sec_day){
    //is in between
}

cause if $first_day = 5, $now = 6 and $sec_day = 0, this would fail even if it is in between. How should I approach this ?

Upvotes: 1

Views: 341

Answers (2)

marconalida
marconalida

Reputation: 51

Only need to alter the days in case the first day is bigger than the last day :

//first correct days
if ($first_day > $sec_day){
    if ($now > $first_day){
        $sec_day+=7;
    }
    if ($now < $first_day)
        $first_day-=7;
    }
}
if ($first_day < $now < $sec_day){
    //is in between
}

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

That's the expected behavior. Saturday isn't found between Friday and Sunday, unless you're starting to speak of different weeks. In which case you need to take date("W") (week number) into account. A simple solution would be to use date("z"); which means the day of the year. You cannot determine if a day is between two days without looking at the greater date.

Upvotes: 0

Related Questions