那么,是否有可能使用php explode隐藏比今天少的日期?例如:
<select class="form-control nwthg mvcomment" name="dt" required>
<?php $tags = explode(",",$res["on_dates"]);foreach ($tags as &$tag) {$tag = "<option value=\"$tag\" >$tag</option>";}echo implode(", ", $tags);?>
</select>
我想显示所有日期,但是当日期早于今天时,它将被隐藏。 我将所有日期存储在mysql中的一行中,用逗号分隔,如下所示:
4-Oct-2019,5-Oct-2019,6-Oct-2019,10-Oct-2019,11-Oct-2019,13-Oct-2019,12-Oct-2019,25-Oct-2019,26-Oct-2019,18-Oct-2019,20-Oct-2019,31-Oct-2019,19-Oct-2019,24-Oct-2019,1-Nov-2019,2-Nov-2019,7-Nov-2019,10-Nov-2019,17-Nov-2019,19-Nov-2019
是的,我想隐藏所有少于今天的日期
答案 0 :(得分:1)
有几种方法可以做到这一点。
其中之一是按照Kevin的建议进行操作,并在MYSQL查询中使用WHERE
子句。
如果您真的必须以其他方式隐藏它们,则只需执行以下操作即可:
$dates = '4-Oct-2019,5-Oct-2019,6-Oct-2019,10-Oct-2019,11-Oct-2019,13-Oct-2019,12-Oct-2019,25-Oct-2019,26-Oct-2019,18-Oct-2019,20-Oct-2019,31-Oct-2019,19-Oct-2019,24-Oct-2019,1-Nov-2019,2-Nov-2019,7-Nov-2019,10-Nov-2019,17-Nov-2019,19-Nov-2019';
$tags = explode(',', $dates);
foreach ($tags as $id => $tag) {
$date = strtotime($tag);
if ($date < strtotime(date('d-M-Y'))) {
unset($tags[$id]);
} else {
$tags[$id] = "<option value=\"$tag\" >$tag</option>";
}
}
echo implode(",", $tags);
答案 1 :(得分:1)
使用DateTime,array_filter,array_map和closure,您可以解决以下过滤问题:
$dates = '4-Oct-2019,5-Oct-2019,6-Oct-2019,10-Oct-2019,11-Oct-2019,13-Oct-2019,12-Oct-2019,25-Oct-2019,26-Oct-2019,18-Oct-2019,20-Oct-2019,31-Oct-2019,19-Oct-2019,24-Oct-2019,1-Nov-2019,2-Nov-2019,7-Nov-2019,10-Nov-2019,17-Nov-2019,19-Nov-2019';
$tags = explode(',', $dates);
$now = new DateTime('NOW');
$new = array_map(function($item) {
return "<option value=\"$item\">$item</option>";
}, array_filter($tags, function($date) use ($now) {
return DateTime::createFromFormat('j-M-Y', $date) < $now;
})
);
print_r($new);
结果:
Array
(
[0] => <option value="4-Oct-2019" >4-Oct-2019</option>
[1] => <option value="5-Oct-2019" >5-Oct-2019</option>
[2] => <option value="6-Oct-2019" >6-Oct-2019</option>
[3] => <option value="10-Oct-2019" >10-Oct-2019</option>
[4] => <option value="11-Oct-2019" >11-Oct-2019</option>
[5] => <option value="13-Oct-2019" >13-Oct-2019</option>
[6] => <option value="12-Oct-2019" >12-Oct-2019</option>
[7] => <option value="25-Oct-2019" >25-Oct-2019</option>
[8] => <option value="26-Oct-2019" >26-Oct-2019</option>
[9] => <option value="18-Oct-2019" >18-Oct-2019</option>
[10] => <option value="20-Oct-2019" >20-Oct-2019</option>
[11] => <option value="31-Oct-2019" >31-Oct-2019</option>
[12] => <option value="19-Oct-2019" >19-Oct-2019</option>
[13] => <option value="24-Oct-2019" >24-Oct-2019</option>
[14] => <option value="1-Nov-2019" >1-Nov-2019</option>
[15] => <option value="2-Nov-2019" >2-Nov-2019</option>
[16] => <option value="7-Nov-2019" >7-Nov-2019</option>
[17] => <option value="10-Nov-2019" >10-Nov-2019</option>
)
生成所需的标记:
printf('<select class="form-control nwthg mvcomment" name="dt" required>%s</select>', implode("", $new));
结果:
<select class="form-control nwthg mvcomment" name="dt" required><option value="4-Oct-2019">4-Oct-2019</option><option value="5-Oct-2019">5-Oct-2019</option><option value="6-Oc
t-2019">6-Oct-2019</option><option value="10-Oct-2019">10-Oct-2019</option><option value="11-Oct-2019">11-Oct-2019</option><option value="13-Oct-2019">13-Oct-2019</option><opt
ion value="12-Oct-2019">12-Oct-2019</option><option value="25-Oct-2019">25-Oct-2019</option><option value="26-Oct-2019">26-Oct-2019</option><option value="18-Oct-2019">18-Oct-
2019</option><option value="20-Oct-2019">20-Oct-2019</option><option value="31-Oct-2019">31-Oct-2019</option><option value="19-Oct-2019">19-Oct-2019</option><option value="24-
Oct-2019">24-Oct-2019</option><option value="1-Nov-2019">1-Nov-2019</option><option value="2-Nov-2019">2-Nov-2019</option><option value="7-Nov-2019">7-Nov-2019</option><option value="10-Nov-2019">10-Nov-2019</option></select>