Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers

up vote 2 down vote accepted

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)) {
   ...
}
share|improve this answer
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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.