Reputation: 437
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
Upvotes: 0
Views: 1698
Reputation: 6647
function dateIsAllowedWeekday($_date,$_allowed){
if ((isDate($_date)) && (($_allowed!="null") && ($_allowed!=null))){
return in_array(date("w",strtotime($_date)),json_decode($_allowed));
}
return false;
}
Upvotes: 0
Reputation: 37085
The reason why it keeps returning Thursday is because you are using strtotime()
on single digit integer:
$weekday=date("l",date("w",strtotime($wd)));
One of the two things is happening, thought I'm not sure which:
the function interprets the integer as an epoch timestamp, or
the function is returning false, which the date
function gets as a 0
.
In either case, you are within 10 seconds of the original Epoch start time:
Thu, 01 Jan 1970 00:00:00 GMT
Which is on a Thursday.
Upvotes: 1
Reputation: 317197
If you got PHP5.3 running already, you can also do:
function date_validWeekday($format, $date, array $weekdays) {
return in_array(
DateTime::createFromFormat($format, $date)->format('w'),
$weekdays);
}
var_dump(date_validWeekday('d.m.Y', '21.05.2010', array(0,1,2))); // FALSE
var_dump(date_validWeekday('d.m.Y', '11.05.2010', array(0,1,2))); // TRUE
Upvotes: 1
Reputation: 27102
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:
function dateIsAllowedWeekday($_date,$_allowed)
{
if ((isDate($_date)) && (($_allowed!="null") && ($_allowed!=null)))
{
$allowed_weekdays = json_decode($_allowed);
if (in_array(date("w", strtotime($_date)), $allowed_weekdays))
{
return TRUE;
}
}
return FALSE;
}
Tested with 21.05.2010
(returns false), and 11.05.2010
(returns true), with your allowed_weekdays as above ([0,1,2]
).
Upvotes: 2