检查变量是星期六还是星期日

时间:2013-11-14 10:35:18

标签: php

我正在尝试使用一个脚本来检查$ acdate`是星期六还是星期日,如果是,它应该用新的类改变当前的类。但我不是因为某种原因让它工作,我尝试了不同的方法,寻找可能的答案让它工作,但最后我不得不试着你们看看你是否有可能解决问题的方法 如果您想知道这是我的代码

,则行caseclosure会返回0-9之间的值
<?php
$acdate = 0;
while ($row = mysql_fetch_assoc($tccrequest))
{
    $acdate = date('d-m-Y',time() + 86400 * $row['autoclosure']);
    if($row['ac update']!=1){
        if ($acdate <= date('d-m-Y')){
            $warning= "warning2";
        }
        else if ($acdate == date('d-m-Y')+1){
            $warning= "nextday";
        }
        else if ($acdate == strtotime('this Saturday')){
            $warning= "warning2";
        }
        else if ($acdate == strtotime('this Sunday')){
            $warning= "warning2";
        }
        else{
            $warning="";
            $disable = "disabled=\"disabled\"";
        }
    }else{
            $warning="updated";
            //$disable = "disabled=\"disabled\"";           
    }
?>

2 个答案:

答案 0 :(得分:3)

只需使用date功能

即可
$Datetime_acdate  = strtotime(acdate);

//will return string 'Sat' or 'Sun' or 'Mon' etc 
$DayofWeek = date('D', $Datetime_acdate );

if ($DayofWeek == 'Sat' or $DayofWeek == 'Sun'){
 //do something.
}

你应该在php.net上的date功能页面上使用speeddial !!

答案 1 :(得分:0)

strtotime('this Saturday')将返回当前星期日对应的Unix时间戳。但是您的$acdate变量是日期字符串,因此使用strtotime()进行比较将永远不会起作用。在进行比较之前,您必须将日期转换为时间戳:

更改:

$acdate = date('d-m-Y',time() + 86400 * $row['autoclosure']);

为:

$acdate = time() + 86400 * $row['autoclosure'];

但是如果你试图检查日期是否是一个星期日(无论它在哪一周),你可以简单地使用l格式(小写L)`:

$acdateTS = strtotime($acdate);
if(date('l', $acdateTS) == 'Sunday') {
    // do something
}
相关问题