开发者

How to limit results to only future date in php

How to limit result to today date.

E.g. we have three dates in this format (Y-m-d H:i:s):

$a = "2011-08-10 23:55:01";
$b = "2011-08-10 00:20:01";
$c = "2011-08-10 09:45:01";

and I don't want to 开发者_如何学编程echo dates that are past. I want just future dates to be shown.

How to make if then statement for future dates only?


Use the strtotime function and compare with the current time given by the time function:

if (strtotime($a) > time()) {
  echo $a;
}


Because I can't see if you're using this in a function somewhere, I chose to concatenate the dates into an array, and work with the array using a foreach:

$a = "2011-08-10 23:55:01";
$b = "2011-08-10 00:20:01";
$c = "2012-08-10 09:45:01";

$dates = array($a,$b,$c);

echo "<ul>";
foreach($dates as $k => $v) {
    if (strtotime($v) > time()){
        echo "<li>$v</li>";
        }
    }
echo "</ul>";

The above, at the time of writing, outputs:

  • 2011-08-10 23:55:01
  • 2012-08-10 09:45:01


if (strtotime($a) > time()) {
    echo $a;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜