Correct usage of the MySQL Date function

Source: Internet
Author: User

The following articles mainly introduce the practical application of the MySQL Date function, including how to obtain the specific operation of the current time and the specific application of the Unix time, the following describes the actual content before and after the time, the time interval, and the time conversion.

MySQL Date function 1. Get the current time

 
 
  1. MySQL> select current_timestamp();  
  2. +---------------------+  
  3. | current_timestamp() |  
  4. +---------------------+  
  5. | 2010-01-18 21:24:37 |  
  6. +---------------------+  
  7. 1 row in set (0.00 sec)  
  8. MySQL> select current_date();  
  9. +----------------+  
  10. | current_date() |  
  11. +----------------+  
  12. | 2010-01-18 |  
  13. +----------------+  
  14. 1 row in set (0.00 sec)  
  15. MySQL> select current_time();  
  16. +----------------+  
  17. | current_time() |  
  18. +----------------+  
  19. | 21:24:46 |  
  20. +----------------+  
  21. 1 row in set (0.00 sec)  

MySQL Date function 2. Unix time

 
 
  1. MySQL> select unix_timestamp();  
  2. +------------------+  
  3. | unix_timestamp() |  
  4. +------------------+  
  5. | 1263821184 |  
  6. +------------------+  
  7. 1 row in set (0.00 sec)  
  8. MySQL> select from_unixtime(1263821182);  
  9. +---------------------------+  
  10. | from_unixtime(1263821182) |  
  11. +---------------------------+  
  12. | 2010-01-18 21:26:22 |  
  13. +---------------------------+  
  14. 1 row in set (0.00 sec)  

MySQL Date function 3. Before and after the time

 
 
  1. MySQL> select date_add(current_timestamp, interval 1 day);  
  2. +---------------------------------------------+  
  3. | date_add(current_timestamp, interval 1 day) |  
  4. +---------------------------------------------+  
  5. | 2010-01-19 21:27:53 |  
  6. +---------------------------------------------+  
  7. 1 row in set (0.00 sec)  
  8. MySQL> select date_add(current_time, interval 1 day);  
  9. +----------------------------------------+  
  10. | date_add(current_time, interval 1 day) |  
  11. +----------------------------------------+  
  12. | NULL |  
  13. +----------------------------------------+  
  14. 1 row in set, 1 warning (0.00 sec)  
  15. MySQL> select date_add(current_date, interval 1 day);  
  16. +----------------------------------------+  
  17. | date_add(current_date, interval 1 day) |  
  18. +----------------------------------------+  
  19. | 2010-01-19 |  
  20. +----------------------------------------+  
  21. 1 row in set (0.00 sec)  
  22. MySQL> select date_sub(current_timestamp, interval 1 day);  
  23. +---------------------------------------------+  
  24. | date_sub(current_timestamp, interval 1 day) |  
  25. +---------------------------------------------+  
  26. | 2010-01-17 21:28:41 |  
  27. +---------------------------------------------+  
  28. 1 row in set (0.00 sec)  
  29. MySQL> select date_sub(current_date, interval 1 day);  
  30. +----------------------------------------+  
  31. | date_sub(current_date, interval 1 day) |  
  32. +----------------------------------------+  
  33. | 2010-01-17 |  
  34. +----------------------------------------+  
  35. 1 row in set (0.00 sec)  
  36. MySQL> select date_sub(current_time, interval 1 day);  
  37. +----------------------------------------+  
  38. | date_sub(current_time, interval 1 day) |  
  39. +----------------------------------------+  
  40. | NULL |  
  41. +----------------------------------------+  
  42. 1 row in set, 1 warning (0.00 sec)  

 

MySQL Date function 4. Time Interval

 
 
  1. MySQL> select datediff('2010-01-18','2010-01-17');  
  2. +-------------------------------------+  
  3. | datediff('2010-01-18','2010-01-17') |  
  4. +-------------------------------------+  
  5. | 1 |  
  6. +-------------------------------------+  
  7. 1 row in set (0.00 sec)  
  8. MySQL> select timediff('2010-01-18 12:00','2010-01-17 11:00');  
  9. +-------------------------------------------------+  
  10. | timediff('2010-01-18 12:00','2010-01-17 11:00') |  
  11. +-------------------------------------------------+  
  12. | 25:00:00 |  
  13. +-------------------------------------------------+  
  14. 1 row in set (0.00 sec)  

MySQL Date function 5. Time Conversion

 
 
  1. MySQL> select time_to_sec('25:00:00');  
  2. +-------------------------+  
  3. | time_to_sec('25:00:00') |  
  4. +-------------------------+  
  5. | 90000 |  
  6. +-------------------------+  
  7. 1 row in set (0.00 sec)  
  8. MySQL> select sec_to_time(90000);  
  9. +--------------------+  
  10. | sec_to_time(90000) |  
  11. +--------------------+  
  12. | 25:00:00 |  
  13. +--------------------+  
  14. 1 row in set (0.00 sec)   

The above content is an introduction to the MySQL Date function. I hope you will get some benefits.

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.