Skip to content

Instantly share code, notes, and snippets.

@psaunders88
Created November 8, 2013 18:03
Show Gist options
  • Save psaunders88/7375058 to your computer and use it in GitHub Desktop.
Save psaunders88/7375058 to your computer and use it in GitHub Desktop.
A function to add date clauses Zend_Db_Select filtering by day/month/year
/**
* Add clauses for date values chosen to select
*
* @param Zend_Db_Select $oSelect
* @param integer $day
* @param integer $month
* @param integer $year
*
* @return Zend_Db_Select
*/
public function _addDateClauses($oSelect, $day, $month, $year)
{
// If all three are set then we need to get events running over a specific date
if ($day && $month && $year) {
$date = sprintf("%d-%02d-%02d 00:00:00",$year, $month, $day);
$oSelect->where('? BETWEEN start_date AND d.end_date', $date);
}else if (!$day && $month && $year){
// If the day is not set then assume we want all events from the month provided
$start_date = sprintf("%d-%02d-%02d 00:00:00",$year, $month, 1);
$end_date = new Zend_Date($start_date);
$end_date->add(1, Zend_Date::MONTH);
$condition = sprintf(
'(start_date BETWEEN "%1$s" AND "%2$s" || d.end_date BETWEEN "%1$s" AND "%2$s")',
$start_date,
$end_date->toString('yyyy-MM-dd HH:mm:s')
);
$oSelect->where($condition);
}else{
// Assume we want events that are in future - this is the default
$date = date('Y-m-d 00:00:00');
$oSelect->where('? > start_date && ? < d.end_date', $date);
}
return $oSelect;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment