mysql get current date and format

MYSQL Get the current date and date format
Get the system date: NOW()

1. The DATE_FORMAT() function is used to display date/time data in different formats.

Format date: DATE_FORMAT(date, format)
Note: date: time field
format: date format

select now();

Return system date, output: 2016-12-25 14:38:59

select date_format(now(),'%y-%m-%d');
select date_format(now(),'%Y-%m-%d');

Output: 16-12-25
Output: 2016-12-25

The format of the format parameter is:
%a abbreviated week name
%b abbreviated month name
%c month, value
%D month day with English prefix
%d month day, value (00-31)
%e month day, value(0-31)
%f microsecond
%H hour(00-23)
%h hour(01-12)
%I hour(01-12)
%i minute, value(00-59)
%j day of year ( 001-366)
%k hours (0-23)
%l hours (1-12)
%M month name
%m month, value (00-12)
%p AM or PM
%r time, 12-hour (hh:mm :ss AM or PM)
%S seconds (00-59)
%s seconds (00-59)
%T time, 24-hour (hh:mm:ss)
%U week (00-53) Sunday is the first of the week Day
%u Week (00-53) Monday is the first day of the week
%V Week (01-53) Sunday is the first day of the week, with %X use
%v Week (01-53) Monday is the first day of the week Day 1, with %x use
%W week name
%w day of week (0=Sunday, 6=Saturday)
%X year, where sunday is the first day of the week, 4 digits, with %V use
%x year, where Monday is the first day of the week, 4 digits, with %v use
%Y year, 4 digits
% y year, 2 digits

2. MySQL format function FROM_UNIXTIME()

1、FROM_UNIXTIME( unix_timestamp )

  Parameters: Usually a 10-digit timestamp, such as: 1417363200
  Return value: There are two kinds, it may be a string like 'YYYY-MM-DD HH:MM:SS', or it may be a string like YYYYMMDDHHMMSS.uuuuuu Number, what exactly is returned depends on the form in which the function is called.

mysql> select FROM_UNIXTIME(1344887103);  
+---------------------------+  
| FROM_UNIXTIME(1344887103) |  
+---------------------------+  
| 2012-08-14 03:45:03       |  
+---------------------------+ 

2、FROM_UNIXTIME( unix_timestamp ,format )

  Parameter unix_timestamp: the same as the parameter in the method FROM_UNIXTIME( unix_timestamp);
  parameter format: the format of the converted time string display;
  return value: the string displayed according to the specified time format;

mysql> select FROM_UNIXTIME(1344887103,'%Y-%m-%D %h:%i:%s');  
+-----------------------------------------------+  
| FROM_UNIXTIME(1344887103,'%Y-%m-%D %h:%i:%s') |  
+-----------------------------------------------+  
| 2012-08-14th 03:45:03                         |  
+-----------------------------------------------+ 

3. MySQL date and time calculation function

1. MySQL adds a time interval to the date: date_add()

set @dt = now();

select date_add(@dt, interval 1 day); -- add 1 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select date_add(@dt, interval 1 minute); -- ...
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);

select date_add(@dt, interval -1 day); -- sub 1 day

In MySQL, the adddate(), addtime() functions can be replaced by date_add(). The following is an example of date_add() implementing the addtime() function:

mysql> set @dt = '2008-08-09 12:12:33';
mysql> select date_add(@dt, interval '01:15:30' hour_second);
+------------------------------------------------+
| date_add(@dt, interval '01:15:30' hour_second) |
+------------------------------------------------+
| 2008-08-09 13:28:03                            |
+------------------------------------------------+

mysql> select date_add(@dt, interval '1 01:15:30' day_second);
+-------------------------------------------------+
| date_add(@dt, interval '1 01:15:30' day_second) |
+-------------------------------------------------+
| 2008-08-10 13:28:03                             |
+-------------------------------------------------+

The date_add() function adds "1 hour, 15 minutes, 30 seconds" and "1 day, 1 hour, 15 minutes, 30 seconds" to @dt, respectively.
Recommendation: Always use the date_add() datetime function instead of adddate(), addtime().

2. MySQL subtracts a time interval from a date: date_sub()

mysql> select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
+----------------------------------------------------------------+
| date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second) |
+----------------------------------------------------------------+
| 1997-12-30 22:58:59                                            |
+----------------------------------------------------------------+
-- 得到昨天的日期
select DATE_FORMAT(DATE_SUB('2017-09-01',INTERVAL 1 DAY),'%Y/%m/%d') as yesterday;
+------------+
| yesterday  |
+------------+
| 2017/08/31 |
+------------+

The MySQL date_sub() datetime function is used in the same way as date_add().
There are also two functions subdate() and subtime() in MySQL. It is recommended to use date_sub() instead.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325703198&siteId=291194637