I am experimenting with the date and in_array functions of PHP.
Based on what I have read in the manual I can't understand why my code is returning the else part of the if statement. If the date('D') is returning Tue why doesn't it run the if part?
<?php
date_default_timezone_set('UTC');
$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun");
$today = date('D');
if(in_array("Mon", "Tue", "Wed", "Thur", "Fri", $weekdays) == $today)
{
echo "It's" . " ";
echo $today;
echo " " . "Get out of bed and go to work";
}else{
echo "Do whatever you want becuase it's" . " ";
echo $today;
};
?>
I've tried various things and and changed the if part to this, but to no avail.
if(in_array(array("Mon", "Tue", "Wed", "Thur", "Fri"), $weekdays) == $today)
Can someone tell me what is wrong with the syntax?
if(in_array($today, $weekdays))
{
echo "It's" . " ";
echo $today;
echo " " . "Get out of bed and go to work";
}else{
echo "Do whatever you want becuase it's" . " ";
echo $current_day;
};
in_array manual
The in_array
function checks if a value exists in an array, returning true or false .
If you want to know if $today
is a weekday, you would want to do something like:
$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri");
if(in_array($today, $weekdays)) {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.