Fully collected MySQL date and MySQL Time Functions
Date_format (date, Format)
Format the date value based on the format string. The following modifier can be used in the format string: % m month name (January ...... December)
% W name of the Week (Sunday ...... Saturday)
% D indicates the date of the month with an English prefix (1st, 2nd, 3rd, and so on .)
% Y year, number, 4 digits
% Y year, number, 2 digits
% A abbreviated name of the Week (Sun ...... Sat)
% D number of days in the month (00 ...... 31)
% E number of days in the month (0 ...... 31)
% M month, number (01 ...... 12)
% C month, number (1 ...... 12)
% B abbreviated month name (Jan ...... Dec)
% J days in a year (001 ...... 366)
% H hour (00 ...... 23)
% K hour (0 ...... 23)
% H hour (01 ...... 12)
% I hour (01 ...... 12)
% L hour (1 ...... 12)
% I minute, number (00 ...... 59)
% R time, 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
% W days in a week (0 = Sunday ...... 6 = Saturday)
% U Week (0 ...... 52). Sunday is the first day of the week.
% U Week (0 ...... 52) Monday is the first day of the week.
% A text "% ".
All other characters are not interpreted and copied to the result.
Mysql> select date_format ('2017-10-04 22:23:00 ',' % w % m % y ');
-> 'Saturday October 1997'
Mysql> select date_format ('2017-10-04 22:23:00 ',' % H: % I: % s ');
-> '22: 23: 00'
Mysql> select date_format ('2017-10-04 22:23:00 ',
'% D % Y % A % d % m % B % J ');
-> '4th 97 sat 04 10 Oct 123'
Mysql> select date_format ('2017-10-04 22:23:00 ',
'% H % K % I % R % T % S % W ');
-> '22 22 10 10:23:00 PM 22:23:00 6'
In mysql3.23, % is required before the format modifier. In earlier MySQL versions, % is optional.
Time_format (time, Format)
This is used like the preceding date_format () function, but the format string can only contain format modifiers for processing hours, minutes, and seconds.
Other modifiers generate a null value or 0.
Curdate ()
Current_date
Returns today's date value in 'yyyy-MM-DD 'or yyyymmdd format, depending on whether the function is used in a string or numeric context.
Mysql> select curdate ();
-> '2017-12-15'
Mysql> select curdate () + 0;
-> 19971215
Curtime ()
Current_time
Returns the current time value in 'hh: mm: ss' or hhmmss format, depending on whether the function is used in a string or in the context of a number.
Mysql> select curtime ();
-> '23: 50: 26'
Mysql> select curtime () + 0;
-> 235026
Now ()
Sysdate ()
Current_timestamp
Returns the current date and time in 'yyyy-MM-DD hh: mm: ss' or yyyymmddhhmmss format, depending on whether the function is in a string or in a number
The context is used.
Mysql> select now ();
-> '2017-12-15 23:50:26'
Mysql> select now () + 0;
-> 19971215235026
Unix_timestamp ()
Unix_timestamp (date)
If no parameter is called, a Unix timestamp (in seconds starting from '2017-01-01 00:00:00 'GMT) is returned ). If unix_timestamp () is called with a date parameter, it returns the second value starting from '2017-01-01 00:00:00 'GMT. Date can be a number of a date string, A datetime string, a timestamp, or a local time in yymmdd or yyyymmdd format.
Mysql> select unix_timestamp ();
-> 882226357
Mysql> select unix_timestamp ('2017-10-04 22:23:00 ');
-> 875996580
When unix_timestamp is used in a timestamp column, the function will directly accept the value, without the implicit "string-to-Unix-timestamp" transformation http://www.knowsky.com /.
From_unixtime (unix_timestamp)
Returns the value represented by the unix_timestamp parameter in 'yyyy-MM-DD hh: mm: ss' or yyyymmddhhmmss format, depending on whether the function is used in a string or number context.
Mysql> select from_unixtime (875996580 );
-> '2017-10-04 22:23:00'
Mysql> select from_unixtime (875996580) + 0;
-> 19971004222300
From_unixtime (unix_timestamp, Format)
Returns a string representing the Unix time mark, formatted according to the format string. Format can contain the same modifier as the entries listed by the date_format () function.
Mysql> select from_unixtime (unix_timestamp (),
'% Y % d % m % H: % I: % S % x ');
-> '2014 23rd December 03:43:30 x'
Sec_to_time (seconds)
Returns the seconds parameter, which is converted to hour, minute, and second. The value is formatted in 'hh: mm: ss' or hhmmss, depending on whether the function is used in a string or a number context.
Mysql> select sec_to_time (2378 );
-> '00: 39: 38'
Mysql> select sec_to_time (2378) + 0;
-> 3938
time_to_sec (time)
the return time parameter is converted to seconds.
mysql> select time_to_sec ('22: 23: 00');
-> 80580
mysql> select time_to_sec ('00: 39: 38 ');
-> 2378