Home >Database >Mysql Tutorial >How to Add 2 Hours to the Current Time in MySQL?

How to Add 2 Hours to the Current Time in MySQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-04 13:45:11318browse

How to Add 2 Hours to the Current Time in MySQL?

Adjusting Time in MySQL: Adding Hours to the Current Time

In MySQL, manipulating dates and times is crucial for various database operations. One common task is adding a certain amount of time to the current time. This article explores the correct syntax for adding 2 hours to the current time in MySQL.

Query Syntax

The valid query syntax for adding 2 hours to the current time in MySQL is:

SELECT * 
FROM courses 
WHERE DATE_ADD(NOW(), INTERVAL 2 HOUR) > start_time

Explanation

  • NOW(): Returns the current date and time.
  • DATE_ADD(): A function that adds a specified interval to a date or time value.
  • INTERVAL 2 HOUR: Represents an interval of 2 hours.
  • >: The greater-than operator checks if the adjusted time is greater than the start_time field in the courses table.

Example

Consider a courses table with a start_time field to track the start time of each course. To fetch all courses that start within 2 hours of the current time, you would use the following query:

SELECT * 
FROM courses 
WHERE DATE_ADD(NOW(), INTERVAL 2 HOUR) > start_time

Additional Notes

  • The DATE_ADD() function supports various time intervals, such as minutes, hours, days, and years.
  • You can adjust the interval in the DATE_ADD() function to add a different amount of time.
  • For more information on date and time manipulation in MySQL, refer to the official documentation on Date and Time Functions.

The above is the detailed content of How to Add 2 Hours to the Current Time in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn