From my android smartphone application (made by myself) I send a request to a server in Denver to save my location and time. The TIMESTAMP it saves is Denver’s current time (9 hours difference from local time). Now after 16 hours from the last request I wrote in my php script
$query = "SELECT * FROM `tblLoc` WHERE datetime > (CURRENT_TIMESTAMP - 86400)";
so as if I wanted to show me past 24 hours….
THIS WAS MISTAKE!
$query = "SELECT * FROM `tblLoc` WHERE datetime > CURRENT_TIMESTAMP() - INTERVAL 24 HOUR";
in SQL 86400 != 24 HOUR!!!
You need to either change MySQL’s time zone instead of PHP’s, or change your query so that PHP provides the timestamp, like so:
EDIT based on comment
UTC_TIMESTAMP()might be a good way to go, but make sure you are inserting/updating based on this same timestamp as well, and not based onCURRENT_TIMESTAMP().UTC timestamp is based on GMT (Greenwich Mean Time) and is at GMT+0. Dallas is at GMT-6 and Asia/Jerusalem is at GMT+2. So
UTC_TIMESTAMP()always means the current time in GMT timezone, regardless of what your current time zone setting is.CURRENT_TIMESTAMP()means the current time in your current timezone.HOWEVER your results may still not be quite right. They may just look right for the moment, but may be off by a couple hours. But if you are inserting and updating based on
CURRENT_TIMESTAMPand then selecting based onUTC_TIMESTAMPyour results will not be correct.I would suggest one of the following three solutions:
TZenvironment variable with the correct timezone so that MySQL sets that timezone as the default on startup (again, make sure to restart MySQL).