views:

893

answers:

2

I am trying to check if a date has passed or if it is the future. If the end_date has passed then I do not want it to display. end_date is a MySQL timestamp. Right now all news articles are displaying even if their end_date has passed. Here is the code I am using to check if the date has passed:

function dateExp( $timestamp ){

   $exp = intval($timestamp);
   $today = intval( time() );


   if( $exp > today ) return true;
   else return false;

}

Here is the code that gets the articles and displays them:

$qry = "select *    
      from  news
      where  display='Y'
      order by priority, date_added, title";

$news = _execQry($qry);


foreach( $news as $n )
{
   if( dateExp($n['end_date']) ){
      echo '<h3>'. $n['title'] .'</h3>';
      echo '<p>'. $n['story'] ;
      echo '<br />Added on '. dateFormat($n['date_added']) .'</p><br />';
   }

}
+1  A: 

The problem is with the dateExp function. You're missing a $ sign. Because of that PHP interprets today as a constant instead of $today which is the variable you're using to hold the current timestamp..

It should be like this:

function dateExp( $timestamp )
{

$exp = intval($timestamp);
$today = intval( time() );


if( $exp > $today ) return true;
else return false;

}

As a matter of fact you can streamline the function a little bit more:

function dateExp($timestamp)
{
   $timestamp = intval($timestamp);  // make sure we're dealing wit 'numbers'
   return ($timestamp > time());     // return TRUE if $timestamp is in the future
}
i fixed the missing $ and i tried your streamlined version but i still have the same problem with both
Josh Curren
+2  A: 

I suggest that you trim the records inside the query that way you program has less data to process.

$qry = "select *
from news
where display='Y' and end_date >= CurDate()
order by priority, date_added, title";

JD
this also gave me the same problem
Josh Curren
if your field is an INT instead of a DATE or DATETIME field, you could also use end_date >= UNIX_TIMESTAMP()
nickf
end_date is a timestamp
Josh Curren
your first suggestion worked. I had just miss typed and added an '
Josh Curren