如果今天不在两个给定日期之间隐藏项目?

时间:2013-05-24 13:02:18

标签: php date select

当日期不在2个给定日期之间时,我想隐藏选择框中的项目。 例如:startdate是may的第5个,enddate是may的第16个。如果今天是第10天,则可以显示该项目。如果今天是第4天或第24天,则无法显示该项目。

我试过了:

<?php
echo "<select>";        
if(05-05-2013 < date("d-m-Y") AND 16-05-2013 < date("d-m-Y")){
echo "<style type='text/css'>
    .date{
        display: none;
    }
</style>";  
}


echo "<option class='date'>test1</option>";
echo "<option class='date'>test2</option>";

echo "</select>";
?>

和此:

<?php
echo "<option style='
if(05-05-2013 < date('d-m-Y') AND 16-05-2013 < date('d-m-Y'))
{ 
     echo 'display: none;'; 
}'>test</option>";
?>

但这不起作用。有人知道怎么做吗? 提前谢谢!

3 个答案:

答案 0 :(得分:5)

$today = new DateTime();
$start = new DateTime('05-05-2013');
$end   = new DateTime('16-05-2013');

if ($today >= $start && $today <= $end)
{
    // show it
}

答案 1 :(得分:0)

试试这个

  $display_style = '';
if((strtotime('05-05-2013') < strtotime(date("d-m-Y"))) AND (strtotime('16-05-2013') < strtotime(date("d-m-Y")))){
$display_style =  "style='display:none;'";  
}

echo "<select ".$display_style.">";        
echo "<option class='date'>test1</option>";
echo "<option class='date'>test2</option>";

echo "</select>";

请告诉我是否可以为您提供更多帮助。

答案 2 :(得分:0)

问题是“我希望在两个给定日期之间时隐藏项目”,请尝试以下方法:

$date_compare='17-05-2013';
if($date_compare<'05-05-2013'||$date_compare>'16-05-2013'){//if date_compare not between 05-05-2013 and 16-05-2013 show
echo "<style type='text/css'>
        .date{
          display: none;
         }
       </style>";
}

仅在2个给定日期

之间显示日期时的样式