I have set my server s timezone to be UTC. My users can select their own timezones like UTC+2 etc. I have created a query to indicate when a shift is open or closed:
SELECT DAYNAME(NOW()), start_day
FROM Shifts
WHERE (start_day = LOWER(DAYNAME(NOW()))
AND start_time < CURTIME()
AND end_time > CURTIME())
OR (start_day = LOWER(DAYNAME(DATE_SUB(NOW(), INTERVAL 1 DAY)))
AND start_time < ADDTIME('24:00:00', CURTIME())
AND end_time > ADDTIME('24:00:00', CURTIME()))
I want to alter this query, NOW() and CURTIME() to match the settings of each user. How should I alter my query, to inject custom UTC in it? Is there another way to accomplish what I need ?
CONVERT_TZ function will solve your problem.
Syntax
CONVERT_TZ(dt, from_tz, to_tz).
“from_tz” is your server timezone (in your case , UTC).
“to_tz” value can be given as a string indicating an offset from “from_tz”, such as ‘+10:00’ or ‘-6:00’.
Examples: