Subtracting Hours from a Datetime in MySQL
You have a datetime field that you want to subtract 3 hours from due to GMT issues. One way to achieve this is by using the DATE_SUB() MySQL function.
Solution:
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
By replacing column with your datetime field, this query will subtract 3 hours from the specified field.
Alternative Approach:
While DATE_SUB() provides a quick solution, it's important to consider addressing the underlying time zone issue. This approach involves setting the MySQL server time zone to the appropriate GMT value to ensure accurate datetime calculations. This can be achieved using the following statement:
SET TIMEZONE = 'your_time_zone';
By setting the time zone, all datetime fields will automatically adjust to the specified zone, eliminating the need for manual subtractions. This can result in more robust and reliable queries.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3