MySQL time stamp

Source: Internet
Author: User

1. MySQL Gets the current timestamp function: Current_timestamp, Current_timestamp ()
Mysql> Select Current_timestamp, Current_timestamp ();

+---------------------+---------------------+
| Current_timestamp | Current_timestamp () |
+---------------------+---------------------+
| 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
+---------------------+---------------------+

2. MySQL (Unix timestamp, date) conversion function:
Unix_timestamp (),
Unix_timestamp (date),
From_unixtime (Unix_timestamp),
From_unixtime (Unix_timestamp,format)

Here's an example:
Select Unix_timestamp (); --1218290027
Select Unix_timestamp (' 2008-08-08 '); --1218124800
Select Unix_timestamp (' 2008-08-08 12:30:00 '); --1218169800

Select From_unixtime (1218290027); --' 2008-08-09 21:53:47 '
Select From_unixtime (1218124800); --' 2008-08-08 00:00:00 '
Select From_unixtime (1218169800); --' 2008-08-08 12:30:00 '

Select From_unixtime (1218169800, '%Y%d%M%h:%i:%s%x '); --' 8th August 12:30:00 2008 '

3. MySQL timestamp (timestamp) conversion, increment, decrement function:
Timestamp (date)--date to timestamp
Timestamp (dt,time)--DT + Time
Timestampadd (unit,interval,datetime_expr)--
Timestampdiff (UNIT,DATETIME_EXPR1,DATETIME_EXPR2)--

Take a look at the Example section:
Select timestamp (' 2008-08-08 '); --2008-08-08 00:00:00
Select Timestamp (' 2008-08-08 08:00:00 ', ' 01:01:01 '); --2008-08-08 09:01:01
Select Timestamp (' 2008-08-08 08:00:00 ', ' 10 01:01:01 '); --2008-08-18 09:01:01

Select Timestampadd (Day, 1, ' 2008-08-08 08:00:00 '); --2008-08-09 08:00:00
Select Date_add (' 2008-08-08 08:00:00 ', Interval 1 day); --2008-08-09 08:00:00

The MySQL Timestampadd () function is similar to Date_add ().
Select Timestampdiff (year, ' 2002-05-01 ', ' 2001-01-01 '); ---1
Select Timestampdiff (Day, ' 2002-05-01 ', ' 2001-01-01 '); ---485
Select Timestampdiff (Hour, ' 2008-08-08 12:00:00 ', ' 2008-08-08 00:00:00 '); ---12

Select DateDiff (' 2008-08-08 12:00:00 ', ' 2008-08-01 00:00:00 '); --7

The MySQL Timestampdiff () function is much stronger than the DateDiff () function, and DateDiff () can only calculate the number of days between two dates (date).

MySQL time stamp

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.