2

我正在试验 PHP 的datein_array函数。

根据我在手册中阅读的内容,我无法理解为什么我的代码会返回 if 语句的 else 部分。如果date('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)

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

4

2 回答 2

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

于 2013-03-26T19:34:31.687 回答
2

in_array函数检查数组中是否存在值,返回 true 或 false

如果您想知道是否$today是工作日,您需要执行以下操作:

$weekdays = array("Mon", "Tue", "Wed", "Thur", "Fri");
if(in_array($today, $weekdays)) {
   ...
}
于 2013-03-26T19:35:38.243 回答