MySQL Date and Time - Functions and Formats
MySQL Date and Time - Functions and Formats
CURRENT_DATE(),
Synonyms for CURDATE()
CURRENT_DATE
Sameer Dehadrai
FROM_UNIXTIME() Formats date as a UNIX timestamp
LOCALTIMESTAMP,
Synonym for NOW()
LOCALTIMESTAMP()
MAKETIME MAKETIME()
When invoked with the days form of the second argument, MySQL treats it as an integer
number of days to be added to expr.
ADDTIME(expr1,expr2)
ADDTIME() adds expr2 to expr1 and returns the result. expr1 is a time or datetime expression
and expr2 is a time expression.
CONVERT_TZ(dt,from_tz,to_tz)
This converts a datetime value dt from the time zone given by from_tz to the time zone given
by to_tz and returns the resulting value. This function returns NULL if the arguments are
invalid.
CURDATE()
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.
CURTIME()
Returns the current time as a value in 'HH:MM:SS' or HHMMSS format, depending on whether
the function is used in a string or numeric context. The value is expressed in the current time
zone.
mysql> SELECT CURTIME();
+---------------------------------------------------------+
| CURTIME() |
+---------------------------------------------------------+
| 23:50:26 | +---------------------
------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CURTIME() +
0;
+---------------------------------------------------------+
| CURTIME() + 0 |
+---------------------------------------------------------+
| 235026 | +---------------------
------------------------------------+
1 row in set (0.00 sec)
DATE(expr)
Extracts the date part of the date or datetime expression expr.
DATEDIFF(expr1,expr2)
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.
The INTERVAL keyword and the unit specifier are not case sensitive.
The following table shows the expected form of the expr argument for each unit value;
unit Value ExpectedexprFormat
MICROSECOND MICROSECONDS
SECOND SECONDS
MINUTE MINUTES
HOUR HOURS
DAY DAYS
WEEK WEEKS
MONTH MONTHS
QUARTER QUARTERS
YEAR YEARS
SECOND_MICROSECOND 'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND 'MINUTES.MICROSECONDS'
MINUTE_SECOND 'MINUTES:SECONDS'
HOUR_MICROSECOND 'HOURS.MICROSECONDS'
HOUR_SECOND 'HOURS:MINUTES:SECONDS'
HOUR_MINUTE 'HOURS:MINUTES'
DAY_MICROSECOND 'DAYS.MICROSECONDS'
YEAR_MONTH 'YEARS-MONTHS'
The values QUARTER and WEEK are available beginning with MySQL 5.0.0.
mysql> SELECT DATE_ADD('1997-12-31 23:59:59',
-> INTERVAL '1:1' MINUTE_SECOND);
+---------------------------------------------------------+
| DATE_ADD('1997-12-31 23:59:59', INTERVAL... |
+---------------------------------------------------------+
| 1998-01-01 00:01:00 | +---------------------
------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATE_ADD('1999-01-01', INTERVAL 1
HOUR);
+---------------------------------------------------------+
| DATE_ADD('1999-01-01', INTERVAL 1 HOUR) |
+---------------------------------------------------------+
| 1999-01-01 01:00:00 | +---------------------
------------------------------------+
1 row in set (0.00 sec)
DATE_FORMAT(date,format)
Formats the date value according to the format string.
The following specifiers may be used in the format string. The .%. character is required before
format specifier characters.
Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, .)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%k Hour (0..23)
%l Hour (1..12)
%p AM or PM
%S Seconds (00..59)
%s Seconds (00..59)
DAY(date)
DAY() is a synonym for DAYOFMONTH().
DAYNAME(date)
Returns the name of the weekday for date.
DAYOFMONTH(date)
Returns the day of the month for date, in the range 0 to 31.
DAYOFWEEK(date)
Returns the weekday index for date (1 = Sunday, 2 = Monday, ., 7 = Saturday). These index
values correspond to the ODBC standard.
DAYOFYEAR(date)
Returns the day of the year for date, in the range 1 to 366.
FROM_DAYS(N)
Given a day number N, returns a DATE value.
Use FROM_DAYS() with caution on old dates. It is not intended for use with values that
precede the advent of the Gregorian calendar (1582).
FROM_UNIXTIME(unix_timestamp)
FROM_UNIXTIME(unix_timestamp,format)
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD
HH:MM:SS' or YYYYMMDDHHMMSS format, depending on whether the function is used in a
string or numeric context. The value is expressed in the current time zone. unix_timestamp
is an internal timestamp value such as is produced by the UNIX_TIMESTAMP() function.
If format is given, the result is formatted according to the format string, which is used the
same way as listed in the entry for the DATE_FORMAT() function.
HOUR(time)
Returns the hour for the time. The range of the return value is 0 to 23 for time-of-day values.
However, the range of TIME values actually is much larger, so HOUR can return values greater
than 23.
LAST_DAY(date)
Takes a date or datetime value and returns the corresponding value for the last day of the
month. Returns NULL if the argument is invalid.
MAKEDATE(year,dayofyear)
Returns a date, given year and day-of-year values. dayofyear must be greater than 0 or the
result is NULL.
MAKETIME(hour,minute,second)
Returns a time value calculated from the hour, minute and second arguments.
MICROSECOND(expr)
Returns the microseconds from the time or datetime expression expr as a number in the range
from 0 to 999999.
MINUTE(time)
Returns the minute for time, in the range 0 to 59.
MONTH(date)
Returns the month for date, in the range 0 to 12.
MONTHNAME(date)
Returns the full name of the month for date.
PERIOD_ADD(P,N)
Adds N months to period P (in the format YYMM or YYYYMM). Returns a value in the format
YYYYMM. Note that the period argument P is not a date value.
PERIOD_DIFF(P1,P2)
Returns the number of months between periods P1 and P2. P1 and P2 should be in the format
YYMM or YYYYMM. Note that the period arguments P1 and P2 are not date values.
SECOND(time)
Returns the second for time, in the range 0 to 59.
SEC_TO_TIME(seconds)
Returns the seconds argument, converted to hours, minutes, and seconds, as a value in
'HH:MM:SS' or HHMMSS format, depending on whether the function is used in a string or
numeric context.
SUBTIME(expr1,expr2)
SUBTIME() returns expr1 . expr2 expressed as a value in the same format as expr1. expr1 is
a time or datetime expression, and expr2 is a time.
mysql> SELECT SUBTIME('1997-12-31 23:59:59.999999',
-> '1 1:1:1.000002');
+---------------------------------------------------------+
| SUBTIME('1997-12-31 23:59:59.999999'... |
+---------------------------------------------------------+
| 1997-12-30 22:58:58.999997 | +-----
----------------------------------------------------+
1 row in set (0.00 sec)
SYSDATE()
Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or
YYYYMMDDHHMMSS format, depending on whether the function is used in a string or
numeric context.
TIME(expr)
Extracts the time part of the time or datetime expression expr and returns it as a string.
TIMEDIFF(expr1,expr2)
TIMEDIFF() returns expr1 . expr2 expressed as a time value. expr1 and expr2 are time or
date-and-time expressions, but both must be of the same type.
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a
datetime value. With two arguments, it adds the time expression expr2 to the date or datetime
expression expr1 and returns the result as a datetime value.
TIMESTAMPADD(unit,interval,datetime_expr)
Adds the integer expression interval to the date or datetime expression datetime_expr. The
unit for interval is given by the unit argument, which should be one of the following values:
FRAC_SECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER or YEAR.
The unit value may be specified using one of keywords as shown, or with a prefix of SQL_TSI_.
For example, DAY and SQL_TSI_DAY both are legal.
TIME_FORMAT(time,format)
This is used like the DATE_FORMAT() function, but the format string may contain format
specifiers only for hours, minutes, and seconds.
If the time value contains an hour part that is greater than 23, the %H and %k hour format
specifiers produce a value larger than the usual range of 0..23. The other hour format
specifiers produce the hour value modulo 12.
TIME_TO_SEC(time)
Returns the time argument, converted to seconds.
TO_DAYS(date)
Given a date, returns a day number (the number of days since year 0).
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If called with no argument, returns a UNIX timestamp (seconds since '1970-01-01 00:00:00'
UTC) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns
the value of the argument as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE
string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or YYYYMMDD.
mysql> SELECT UNIX_TIMESTAMP();
+---------------------------------------------------------+
| UNIX_TIMESTAMP() |
+---------------------------------------------------------+
| 882226357 | +---------------------
------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT UNIX_TIMESTAMP('1997-10-04
22:23:00');
+---------------------------------------------------------+
| UNIX_TIMESTAMP('1997-10-04 22:23:00') |
+---------------------------------------------------------+
| 875996580 | +---------------------
------------------------------------+
1 row in set (0.00 sec)
UTC_DATE, UTC_DATE()
Returns the current UTC date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending
on whether the function is used in a string or numeric context.
UTC_TIME, UTC_TIME()
Returns the current UTC time as a value in 'HH:MM:SS' or HHMMSS format, depending on
whether the function is used in a string or numeric context.
UTC_TIMESTAMP, UTC_TIMESTAMP()
Returns the current UTC date and time as a value in 'YYYY-MM-DD HH:MM:SS' or
YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric
context.
WEEKDAY(date)
Returns the weekday index for date (0 = Monday, 1 = Tuesday, . 6 = Sunday).
WEEKOFYEAR(date)
Returns the calendar week of the date as a number in the range from 1 to 53. WEEKOFYEAR()
is a compatibility function that is equivalent to WEEK(date,3).
YEAR(date)
Returns the year for date, in the range 1000 to 9999, or 0 for the .zero. date.
YEARWEEK(date), YEARWEEK(date,mode)
Returns year and week for a date. The mode argument works exactly like the mode argument
to WEEK(). The year in the result may be different from the year in the date argument for the
first and the last week of the year.