2013-11-14 94 views
1

我试图让脚本工作,检查$ 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\"";   
    } 
?> 

回答

0

将返回在当前周的周日对应一个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 
} 
+0

如果$ acdate =星期六或星期天,是否没有办法检查日期函数?这是因为我使用$ acdate = date('d-m-Y',time()+ 86400 * $ row ['autoclosure']);写出今天日期+ $行['autoclosure']在一个领域,所以它看起来像10-08-2013我不需要使用strtotime,如果有一个很好的方法来获取date()来检查$ acdate是否星期六或星期天。 – Tman

+0

@ user2955523:在这种情况下,使用不同的变量来存储时间戳。 –

+0

我能用这个小代码解决它感谢所有的帮助。 \t \t \t否则,如果(日期( “W” 的strtotime( “$ acdate”))== 6日( “W” 的strtotime( “$ acdate”))== 0){ \t \t \t \t $ warning =“warning2”; \t \t \t} – Tman

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功能页上的快速拨号!

相关问题