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"; //actually is Friday (5)
$wd="[0,1,2]"; //Sunday,Monday,Tuesday
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. Then, 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
I’m not sure why you feel the need to convert the numeric day of the week into a string (eg “Sunday”) as you only return a boolean value in the end; anyway I’ve removed that part and the below code should function as expected:
Tested with
21.05.2010(returns false), and11.05.2010(returns true), with your allowed_weekdays as above ([0,1,2]).