MYSQL date and time arithmetic operation class

Stamp processing:

Current date and time:

NOW()

 

The current date:

CURDATE()

 

Transfer date time stamp format

FROM_UNIXTIME(unix_timestamp,'%Y-%m-%d %H:%i:%s')

 

Date format to time stamp

SELECT UNIX_TIMESTAMP()

 

Adding:

DATE_ADD(date,INTERVAL expr unit)

DATE_ADD(now(),INTERVAL 1 day)

 

Subtraction:

DATE_SUB(date,INTERVAL expr unit)

 

 

example:

- Today
SELECT DATE_FORMAT (NOW (), ' % Y-% m-% d 00:00:00') AS ' from today';
the SELECT the DATE_FORMAT (the NOW (), 'M-% D%% Y-23 is: 59:59 ') AS' end of today ';

- yesterday
SELECT DATE_FORMAT (DATE_SUB (CURDATE () , INTERVAL 1 DAY), '% Y-% m-% d 00:00:00') AS ' since yesterday';
the SELECT the DATE_FORMAT (for DATE_SUB (CURDATE (), the INTERVAL. 1 DAY), '% Y-% m- % d 23:59:59') AS ' end of the last';

- week
SELECT DATE_FORMAT (DATE_SUB (DATE_SUB (CURDATE (), INTERVAL WEEKDAY (CURDATE ()) DAY), INTERVAL 1 WEEK), '% Y-% m-% d 00:00:00') AS ' week a ';
the SELECT the DATE_FORMAT (SUBDATE (CURDATE (), WEEKDAY (CURDATE ()) +. 1),'% Y-M-% D% 23:59:59 ') the AS' weekend ';

- week
SELECT DATE_FORMAT (SUBDATE (CURDATE () , DATE_FORMAT (CURDATE (), '% w') - 1), '% Y-% m-% d 00:00:00') AS ' Monday' ;
the SELECT the DATE_FORMAT (SUBDATE (CURDATE (), the DATE_FORMAT (CURDATE (), '% W') -. 7), '% Y-M-% D% 23:59:59') the AS 'weekend';
- top This week there will be algorithmic problems, because mysql is in accordance with Sunday as the first day of the week, if the current is Sunday, it will scheduled for the following week.
the SELECT the DATE_FORMAT (DATE_SUB (CURDATE (), INTERVAL WEEKDAY (CURDATE ( )) DAY), '% Y- % m-% d 00:00:00') AS ' Monday';
the SELECT the DATE_FORMAT (DATE_ADD (SUBDATE (CURDATE (), WEEKDAY (CURDATE ())), the INTERVAL. 6 DAY ), '% Y-% m-% d 23:59:59') AS ' weekend';

-- 上月
SELECT DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01 00:00:00') AS '上月初';
SELECT DATE_FORMAT(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), '%Y-%m-%d 23:59:59') AS '上月末';

- month
SELECT DATE_FORMAT (CURDATE (), ' % Y-% m-01 00:00:00') AS ' this month';
the SELECT the DATE_FORMAT (LAST_DAY (CURDATE ()), '%% Y-M-% d 23:59:59 ') AS' end of the month ';

 

 

Format-related

% M month name (January ~ December) 
% W week name (Sunday ~ Saturday) 
% D dated January English prefix (1st, 2nd, 3rd, etc.) % 
In the Y-digital, four 
% y years, digital position 2 
% a abbreviated weekday name (Sun ~ Sat) 
days% d month, numbers (00 to 31) 
the number of days% e month, numbers (0 to 31) 
% m month, numbers (01 to 12 ) 
% c month, numbers (1 - 12) 
% b abbreviated month name (Jan ~ Dec) 
the number of days in the% j year (001 ~ 366) 
% H hour (00 to 23) 
% k h (0 to 23) 
% h hour (01 ~ 12 is) 
% the I h (01 ~ 12 is) 
% L h (. 1 ~ 12 is) 
% I min, number (00 ~ 59) 
% R & lt time of 12 hours (hh: mm: ss [AP ] M
% T time, 24 hours (HH: mm: SS) 
% S seconds (00 ~ 59) 
% S seconds (00 ~ 59) 
% P AM or PM 
number% w of the week days (0 = Sunday ~ 6 = Saturday ) % 
the U-week (0 to 52), where the first day of the week is Sunday 
% u week (0 to 52), where the first day of the week is Monday 
%% a literal% 

Guess you like

Origin www.cnblogs.com/dinol/p/11570253.html