From f486595e13ef66dd16994d60f3dd5199b35d10c9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 20 Jun 2021 16:55:22 +0200 Subject: [PATCH] Fix dolSqlDateFilter --- htdocs/core/lib/date.lib.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/htdocs/core/lib/date.lib.php b/htdocs/core/lib/date.lib.php index 0dde3a93159..864fea050a0 100644 --- a/htdocs/core/lib/date.lib.php +++ b/htdocs/core/lib/date.lib.php @@ -294,32 +294,35 @@ function convertSecondToTime($iSecond, $format = 'all', $lengthOfDay = 86400, $l /** - * Generate a SQL string to make a filter into a range (for second of date until last second of date) + * Generate a SQL string to make a filter into a range (for second of date until last second of date). + * This method allows to maje SQL request that will deal correctly the timezone of server. * * @param string $datefield Name of SQL field where apply sql date filter * @param int $day_date Day date * @param int $month_date Month date * @param int $year_date Year date * @param int $excludefirstand Exclude first and + * @param mixed $gm False or 0 or 'tzserver' = Return date to compare with server TZ, * @return string $sqldate String with SQL filter */ -function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0) +function dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand = 0, $gm = false) { global $db; $sqldate = ""; if ($month_date > 0) { if ($year_date > 0 && empty($day_date)) { - $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, false)); - $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, false))."'"; + $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, $month_date, $gm)); + $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, $month_date, $gm))."'"; } elseif ($year_date > 0 && !empty($day_date)) { - $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date)); - $sqldate .= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date))."'"; + $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month_date, $day_date, $year_date, $gm)); + $sqldate .= "' AND '".$db->idate(dol_mktime(23, 59, 59, $month_date, $day_date, $year_date, $gm))."'"; } else { + // This case is not reliable on TZ, but we should not need it. $sqldate .= ($excludefirstand ? "" : " AND ")." date_format( ".$datefield.", '%c') = '".$db->escape($month_date)."'"; } } elseif ($year_date > 0) { - $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, false)); - $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, 12, false))."'"; + $sqldate .= ($excludefirstand ? "" : " AND ").$datefield." BETWEEN '".$db->idate(dol_get_first_day($year_date, 1, $gm)); + $sqldate .= "' AND '".$db->idate(dol_get_last_day($year_date, 12, $gm))."'"; } return $sqldate; }