diff --git a/db_objects/person_query.class.php b/db_objects/person_query.class.php index f68a2214..1bc47ae0 100644 --- a/db_objects/person_query.class.php +++ b/db_objects/person_query.class.php @@ -824,24 +824,32 @@ function getSQL($select_fields=NULL) 'after' => Array(1, $length+1) ); list($so, $eo) = $offsets[$values['periodanchor']]; - $between = 'BETWEEN CURDATE() + INTERVAL '.$so.' DAY AND CURDATE() + INTERVAL '.$eo.' DAY'; + if ($so > 0) $so = "+$so"; + if ($eo > 0) $eo = "+$eo"; + $from = date('Y-m-d', strtotime("{$so} days")); + $to = date('Y-m-d', strtotime("{$eo} days")); } else { - $between = 'BETWEEN '.$db->quote($values['from']).' AND '.$db->quote($values['to']); + $from = $values['from']; + $to = $values['to']; } + $betweenExp = 'BETWEEN '.$db->quote($from).' AND '.$db->quote($to); + $valExp = 'pd'.$fieldid.'.value_date'; $w = Array(); - $w[] = '(pd'.$fieldid.'.`value_date` NOT LIKE "-%" - AND pd'.$fieldid.'.`value_date` '.$between.')'; + $w[] = "$valExp NOT LIKE '-%' AND $valExp $betweenExp"; if ($values['criteria'] == 'anniversary') { - // Anniversary matches either have no year or a year before the 'to' year - // AND their month-day fits the range either in the from year or the to year. - $fromyearbetween = 'CONCAT('.$db->quote(substr($values['from'], 0, 4)).', RIGHT(pd'.$fieldid.'.`value_date`, 6)) '.$between; - $toyearbetween = 'CONCAT('.$db->quote(substr($values['to'], 0, 4)).', RIGHT(pd'.$fieldid.'.`value_date`, 6)) '.$between; - $w[] = '(pd'.$fieldid.'.`value_date` LIKE "-%" AND '.$fromyearbetween.')'; - $w[] = '(pd'.$fieldid.'.`value_date` LIKE "-%" AND '.$toyearbetween.')'; - $w[] = '(pd'.$fieldid.'.`value_date` NOT LIKE "-%" AND pd'.$fieldid.'.`value_date` < '.$db->quote($values['to']).' AND '.$fromyearbetween.')'; - $w[] = '(pd'.$fieldid.'.`value_date` NOT LIKE "-%" AND pd'.$fieldid.'.`value_date` < '.$db->quote($values['to']).' AND '.$toyearbetween.')'; + $qFromYear = $db->quote(substr($from, 0, 4)); + $qToYear = $db->quote(substr($to, 0, 4)); + + $w[] = "$valExp LIKE '-%' AND ( + CONCAT($qFromYear, $valExp) $betweenExp + OR CONCAT($qToYear, $valExp) $betweenExp + )"; + $w[] = "$valExp NOT LIKE '-%' AND ( + CONCAT($qFromYear, RIGHT($valExp, 6)) $betweenExp + OR CONCAT($qToYear, RIGHT($valExp, 6)) $betweenExp + )"; } - $customFieldWheres[] = '('.implode(' OR ', $w).')'; + $customFieldWheres[] = '(('.implode("\n) OR (\n", $w).'))'; break; } diff --git a/include/general.php b/include/general.php index fb6754c9..0c1b085f 100644 --- a/include/general.php +++ b/include/general.php @@ -79,6 +79,7 @@ function format_datetime($d) function format_date($d, $includeYear=NULL) { + if ($d == '0000-00-00') return ''; $yearless = is_string($d) && ($d[0] == '-'); if (!is_int($d)) { $d = strtotime($yearless ? "2012{$d}" : $d);