qwentinnau
qwentinnau

Reputation: 217

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?

Upvotes: 1

Views: 1009

Answers (3)

Jack Murdoch
Jack Murdoch

Reputation: 2894

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

Upvotes: 0

David Thomas
David Thomas

Reputation: 253318

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

Upvotes: 1

jches
jches

Reputation: 4527

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

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

Upvotes: 3

Related Questions