Leanne Seawright Leanne Seawright - 7 months ago 23
PHP Question

trying to hide an event two days after it's passed

I'm trying to hide an event two days after it's passed in php (using a mysql query). There are two date options. Start date, and End date. I can't seem to figure out how to make the query work.

$query->select('*');
$query->from('#__events_items');
$query->where('date2 >= "'.$today.'"');
$query->where('date2 <= "'.$sixmths.'"');
$query->where('state = 1');
$query->order('date1 asc');


I've tried

$today = @date('Y-m-d');
$enddate = @date('Y-m-d',(strtotime(@date('Y-m-d')."+ 2 days")));


But obviously the end date won't be greater or equal to $enddate.. Any help would be greatly appreciated!

Answer

You can either modify today like so:

$today = date('Y-m-d', strtotime('-2 days'));

Or modify your query like so :

 $query->select('*');
 $query->from('#__events_items');
 $query->where('date2 >= "'.$today.'" - INTERVAL 2 DAY');
 $query->where('date2 <= "'.$sixmths.'"');
 $query->where('state = 1');
 $query->order('date1 asc');

I would say the second is probably better form since having $today represent two days ago would be nasty self documenting code.