MySQL database Date and Time Functions

Source: Internet
Author: User

The following is a summary of the Practical Application Experience of MySQL database date functions and time functions MySQL 5.x, mySQL date functions and time functions appear frequently in practical applications. The following article describes these two functions in detail.

MySQL database obtains the current date and time function:

1.1 obtain the current date + time) function: now ()

 
 
  1. mysql> select now();  
  2. +---------------------+  
  3. | now() |  
  4. +---------------------+  
  5. | 2008-08-08 22:20:46 |  
  6. +---------------------+  

In addition to the now () function, MySQL also has the following functions:

 
 
  1. current_timestamp()  
  2. ,current_timestamp  
  3. ,localtime()  
  4. ,localtime  
  5. ,localtimestamp -- (v4.0.6)  
  6. ,localtimestamp() -- (v4.0.6) 

These datetime functions are equivalent to now (). Considering that the now () function is short and easy to remember, we recommend that you always use now () to replace the functions listed above.

1.2 obtain the current date + time) function: sysdate ()

The sysdate () Date and time function is similar to now (). The difference is that now () is obtained at the execution start time, And sysdate () dynamically obtains the value during function execution. You can see the following example:

 
 
  1. mysql> select now(), sleep(3), now();  
  2. +---------------------+----------+---------------------+  
  3. | now() | sleep(3) | now() |  
  4. +---------------------+----------+---------------------+  
  5. | 2008-08-08 22:28:21 | 0 | 2008-08-08 22:28:21 |  
  6. +---------------------+----------+---------------------+mysql> select sysdate(), sleep(3), sysdate();  
  7. +---------------------+----------+---------------------+  
  8. | sysdate() | sleep(3) | sysdate() |  
  9. +---------------------+----------+---------------------+  
  10. | 2008-08-08 22:28:41 | 0 | 2008-08-08 22:28:44 |  
  11. +---------------------+----------+---------------------+  

We can see that although sleep lasts for 3 seconds, the time values of the now () function are the same. The time values obtained by the sysdate () function differ by 3 seconds. In the MySQL database Manual, sysdate () is described as follows: Return the time at which the function executes.

Sysdate () Date and time functions are rarely used.

2. Get the current date) function: curdate ()

 
 
  1. mysql> select curdate();  
  2. +------------+  
  3. | curdate() |  
  4. +------------+  
  5. | 2008-08-08 |  
  6. +------------+  

The following two date functions are equivalent to curdate ():

Current_date ()
, Current_date3. Get the current time) function: curtime ()

 
 
  1. mysql> select curtime();  
  2. +-----------+  
  3. | curtime() |  
  4. +-----------+  
  5. | 22:41:30 |  

+ ----------- + The following two time functions are equivalent to curtime ():

Current_time ()

, Current_time4. Get the current UTC date and time function:

 
 
  1. utc_date(), utc_time(), utc_timestamp()  
  2. mysql> select utc_timestamp(), utc_date(), utc_time(), now()  
  3. +---------------------+------------+------------+---------------------+  
  4. | utc_timestamp() | utc_date() | utc_time() | now() |  
  5. +---------------------+------------+------------+---------------------+  
  6. | 2008-08-08 14:47:11 | 2008-08-08 | 14:47:11 | 2008-08-08 22:47:11 |  
  7. +---------------------+------------+------------+---------------------+  

Because China is located in the eight eastern time zone, the local time = UTC time + 8 hours. UTC time is very useful when your business involves multiple countries and regions.

Ii. MySQL database Date and Time Extract selection) function.

1. Select each part of the date and time: date, time, year, quarter, month, day, hour, minute, second, microsecond

 
 
  1. set @dt = '2008-09-10 07:15:30.123456';  
  2. select date(@dt); -- 2008-09-10  
  3. select time(@dt); -- 07:15:30.123456  
  4. select year(@dt); -- 2008  
  5. select quarter(@dt); -- 3  
  6. select month(@dt); -- 9  
  7. select week(@dt); -- 36  
  8. select day(@dt); -- 10  
  9. select hour(@dt); -- 7  
  10. select minute(@dt); -- 15  
  11. select second(@dt); -- 30  
  12. select microsecond(@dt);  

1234562. MySQL Extract () function, which can implement similar functions:

 
 
  1. set @dt = '2008-09-10 07:15:30.123456';  
  2.  
  3. select extract(year from @dt); -- 2008  
  4. select extract(quarter from @dt); -- 3  
  5. select extract(month from @dt); -- 9  
  6. select extract(week from @dt); -- 36  
  7. select extract(day from @dt); -- 10  
  8. select extract(hour from @dt); -- 7  
  9. select extract(minute from @dt); -- 15  
  10. select extract(second from @dt); -- 30  
  11. select extract(microsecond from @dt); -- 123456select extract(year_month from @dt); -- 200809  
  12. select extract(day_hour from @dt); -- 1007  
  13. select extract(day_minute from @dt); -- 100715  
  14. select extract(day_second from @dt); -- 10071530  
  15. select extract(day_microsecond from @dt); -- 10071530123456  
  16. select extract(hour_minute from @dt); -- 715  
  17. select extract(hour_second from @dt); -- 71530  
  18. select extract(hour_microsecond from @dt); -- 71530123456  
  19. select extract(minute_second from @dt); -- 1530  
  20. select extract(minute_microsecond from @dt); -- 1530123456  
  21. select extract(second_microsecond from @dt);  

30123456 apart from date () and time (), the MySQL Extract () function must be fully functional. You can also select 'day _ microsecond. Note that we will not only select day and microsecond, but choose from day of the date to microsecond. Powerful enough!

The above content is an introduction to the MySQL database's date function and time function MySQL 5.X). I hope you will have some gains.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.