Date and Time Functions

MySQL gives you a range of date and time related functions. In essence, the types of functions boil down to

  • Get current date and time

  • Format dates or times

  • Extract information (for example, name of a month of a given date)

  • Add and subtract date and time

  • Compare dates and times

It's worth noting that your data doesn't have to be in a MySQL table for you to use date functions! For example, if your programming language doesn't have some of the powerful functions that MySQL has—the capability to add and subtract dates, say—you can perform the query through MySQL just by using a SELECT statement on the data directly.

When passing a date or time to MySQL, it should be formatted slightly differently, depending on whether you are ...

Get Sams Teach Yourself MySQL in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.