How to Add Two Hours to the Current Time in a MySQL Query?
Dec 10, 2024 pm 02:27 PMExtending Time by Two Hours in MySQL
In MySQL, you may encounter the need to manipulate time values in your queries. One common scenario is adding a specific duration to the current time.
Original Query
The following query attempts to retrieve rows where the start_time field of the courses table is less than the current time offset by two hours:
SELECT * FROM courses WHERE (now() + 2 hours) > start_time
Syntax Error
However, this syntax is invalid in MySQL. The now() function returns a timestamp, and directly adding hours to it is not supported.
Corrected Query
To correctly add 2 hours to the current time in MySQL, use the DATE_ADD() function:
SELECT * FROM courses WHERE DATE_ADD(NOW(), INTERVAL 2 HOUR) > start_time
The DATE_ADD() function takes two arguments: the timestamp to be modified (in this case, NOW()) and an INTERVAL specifying the duration and unit of time to be added. In this case, INTERVAL 2 HOUR adds two hours to the current time.
Additional Notes
The DATE_ADD() function provides versatile date and time manipulation capabilities. It supports various intervals, including days, months, and years. Refer to MySQL documentation for more details on date and time functions.
The above is the detailed content of How to Add Two Hours to the Current Time in a MySQL Query?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

How do I configure SSL/TLS encryption for MySQL connections?
