在if / else中使用in_array和date的问题

时间:2013-03-26 19:31:25

标签: php arrays date

我正在尝试使用PHP的datein_array函数。

根据我在手册中阅读的内容,我无法理解为什么我的代码返回if语句的else部分。如果日期('D')返回 Tue ,为什么不运行if部分?

<?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;
};
?>

我尝试过各种各样的事情并将 if 部分更改为此,但无济于事。

if(in_array(array("Mon", "Tue", "Wed", "Thur", "Fri"), $weekdays) == $today)

有人能告诉我语法有什么问题吗?

2 个答案:

答案 0 :(得分:5)

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手册

答案 1 :(得分:2)

in_array函数checks if a value exists in an array, returning true or false

如果您想知道$today是否是工作日,您可能希望执行以下操作:

$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri");
if(in_array($today, $weekdays)) {
   ...
}