Check if date is allowed weekday in php?

Posted by moogeek on Stack Overflow See other posts from Stack Overflow or by moogeek
Published on 2010-05-11T10:36:03Z Indexed on 2010/05/11 10:44 UTC
Read the original article Hit count: 226

Filed under:
|
|

Hello! I'm stuck with a problem how to check if a specific date is within allowed weekdays array in php. For example,

    function dateIsAllowedWeekday($_date,$_allowed)
    {
    if ((isDate($_date)) && (($_allowed!="null") && ($_allowed!=null))){
    $allowed_weekdays=json_decode($_allowed);
    $weekdays=array();

    foreach($allowed_weekdays as $wd){
    $weekday=date("l",date("w",strtotime($wd)));
    array_push($weekdays,$weekday);
    }
    if(in_array(date("l",strtotime($_date)),$weekdays)){return TRUE;}
    else {return FALSE;}
        }
        else {return FALSE;}
    }
 /////////////////////////////   
    $date="21.05.2010";
    $wd="[0,1,2]";

    if(dateIsAllowedWeekday($date,$wd)){echo "$date is within $wd weekday values!";} 
    else{echo "$date isn't within $wd weekday values!"}

I have input dates formatted as "d.m.Y" and an array returned from database with weekday numbers (formatted as 'Numeric representation of the day of the week') like [0,1,2] - (Sunday,Monday,Tuesday).

The returned string from database can be "null", so i check it too. Them, the isDate function checks whether date is a date and it is ok.

I want to check if my date, for example 21.05.2010 is an allowed weekday in this array. My function always returns TRUE and somehow weekday is always 'Thursday' and i don't know why...

Is there any other ways to check this or what can be my error in the code above? thx

© Stack Overflow or respective owner

Related posts about php

Related posts about date