Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
SELECT CURDATE(); // '2008-06-13' SELECT CURDATE() + 0; // 20080613
Returns the current time as a value in 'HH:MM:SS' or HHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
SELECT CURTIME(); // '23:50:26' SELECT CURTIME() + 0;// 235026.000000
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
SELECT NOW(); // '2007-12-15 23:50:26' SELECT NOW() + 0;// 20071215235026.000000
Extracts the date part of the date or datetime expression expr.
SELECT DATE('2003-12-31 01:02:03'); // '2003-12-31'
DATEDIFF() returns expr1 - expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.
SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');// 1 SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');// -31
Formats the date value according to the format string. The "%" character is required before format . Some of useful specifiers may be used in the format string as follows.
Specifier | Description | %a | Abbreviated weekday name |
---|---|
%b | Abbreviated month name |
%c | Month, numeric |
%D | Day of month with English suffix |
%d | Day of month, numeric (00-31) |
%e | Day of month, numeric (0-31) |
%f | Microseconds |
%H | Hour (00-23) |
%h | Hour (01-12) |
%I | Hour (01-12) |
%i | Minutes, numeric (00-59) |
%j | Day of year (001-366) |
%k | Hour (0-23) |
%l | Hour (1-12) |
%M | Month name |
%m | Month, numeric (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) where Sunday is the first day of week |
%u | Week (00-53) where Monday is the first day of week |
%V | Week (01-53) where Sunday is the first day of week, used with %X |
%v | Week (01-53) where Monday is the first day of week, used with %x |
%W | Weekday name |
%w | Day of the week (0=Sunday, 6=Saturday) |
%X | Year of the week where Sunday is the first day of week, four digits, used with %V |
%x | Year of the week where Monday is the first day of week, four digits, used with %v |
%Y | Year, four digits |
%y | Year, two digits |
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y'); // 'Sunday October 2009' SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s'); // '22:23:00' SELECT DATE_FORMAT('1900-10-04 22:23:00', '%D %y %a %d %m %b %j'); // '4th 00 Thu 04 10 Oct 277' SELECT DATE_FORMAT('1997-10-04 22:23:00','%H %k %I %r %T %S %w'); // '22 22 10 10:23:00 PM 22:23:00 00 6' SELECT DATE_FORMAT('1999-01-01', '%X %V'); // '1998 52'
Returns a Unix timestamp (seconds since '1970-01-01 00:00:00' GMT)
SELECT UNIX_TIMESTAMP(); // 882226357 SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19'); // 1196440219
It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL.
SELECT STR_TO_DATE('01,5,2013','%d,%m,%Y'); // '2013-05-01' SELECT STR_TO_DATE('a09:30:17','a%h:%i:%s'); // '09:30:17' SELECT STR_TO_DATE('a09:30:17','%h:%i:%s'); // NULL