Date & Time



Some of the most common function for Date and Time evaluation and computation are listed below and as follows;


Function

Usage 

Hour() - returns the hour value of a stored date

Hour(column)

Minute - returns just the minute value of a stored date

Minute(column)

Second - returns just the current second value of a stored date column

Second(column)

DayName - returns the name of the day for a date value

DayName(column)

DayOfMonth - returns just the numerical day value of a stored date column

DayofMonth(column)

MonthName - returns the name of the month of a stored date column

MonthName(column)

MONTH - returns just the month numeric value of a stored date column

Month(column)

Year - returns just the numeric Year value of a stored date column

Year(column)

ADDDate - returns a value of x units added to a date column

AddDate(column, INTERVAL X TYPE)

SubDate - returns a value of x units subtracted from column

SubDate(column, INTERVAL X TYPE)

CURDate - returns the current date

CURDATE()

CurTime - returns the current time based on computer clock

CURTIME()

NOW - returns the current DATE and TIME, based on the local computer internal clock

NOW()

UNIX_TIMESTAMP - returns the number of seconds since the epoch or since the date specified

UNIX_TIMESTAMP(date)


Note


  • To add 2 hours to a date, you would write

    ex.: 

       ADDDATE(date, INTERVAL 2 Hour)


  • To add 2 weeks from December 31, 2002

     ex.: 

       ADDDate('2002-12-31', INTERVAL 14 Day)


  • To Subtract 15 months from a date

     ex.: 

       SubDate(date, INTERVAL '1-3' Year_Month)


This last query tells MySQL that you want to subtract 1 year and 3 monthss to the value stored in the date column, above