{"id":2871,"date":"2018-07-03T17:25:01","date_gmt":"2018-07-04T00:25:01","guid":{"rendered":"https:\/\/database.guide\/?p=2871"},"modified":"2021-01-28T14:10:36","modified_gmt":"2021-01-28T04:10:36","slug":"time_format-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/time_format-examples-mysql\/","title":{"rendered":"TIME_FORMAT() Examples &#8211; MySQL"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, you can use the <code>TIME_FORMAT()<\/code> function to format a time value.<\/p>\n<p>This function works just like the <a href=\"https:\/\/database.guide\/date_format-examples-mysql\/\"><code>DATE_FORMAT()<\/code><\/a> function, except that the value can only be formatted in\u00a0hours, minutes, seconds, and microseconds.<\/p>\n<p>See the table at the bottom of this article for a list of format specifiers that can be used with this function.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>TIME_FORMAT(time,format)\r\n<\/pre>\n<p>Where <code>time<\/code> is the time value you want formatted, and <code>format<\/code> is the format string (this determines how the time value is actually formatted).<\/p>\n<h2>Example 1 &#8211; Basic Usage<\/h2>\n<p>Here&#8217;s an example to demonstrate.<\/p>\n<pre>SELECT TIME_FORMAT('14:35:27', '%r') AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------+\r\n| Result      |\r\n+-------------+\r\n| 02:35:27 PM |\r\n+-------------+\r\n<\/pre>\n<p>This example uses the <code>%r<\/code> format specifier, which formats the time as 12-hour (hh:mm:ss followed by AM or PM).<\/p>\n<h2>Example 2 &#8211; More Specific Formatting<\/h2>\n<p>If using a more general format specifier like in the previous example isn&#8217;t suitable, there are various other format specifiers that can help you build the format you require.<\/p>\n<p>For example, if you like the previous format, but you don&#8217;t want the seconds to be returned, you could do this:<\/p>\n<pre>SELECT TIME_FORMAT('14:35:27', '%h:%i %p') AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n+----------+\r\n| 02:35 PM |\r\n+----------+\r\n<\/pre>\n<h2>Example 3 &#8211; 24 Hour Time<\/h2>\n<p>There are also format specifiers for 24 hour time. Here&#8217;s the easiest way to format the time using 24 hour time.<\/p>\n<pre>SELECT TIME_FORMAT('14:35:27', '%T') AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+----------+\r\n| Result   |\r\n+----------+\r\n| 14:35:27 |\r\n+----------+\r\n<\/pre>\n<h2>Example 4 &#8211; Microseconds<\/h2>\n<p>Here&#8217;s an example that includes the fractional seconds part in the result:<\/p>\n<pre>SELECT TIME_FORMAT('14:35:27', '%H:%i:%s.%f') AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-----------------+\r\n| Result          |\r\n+-----------------+\r\n| 14:35:27.000000 |\r\n+-----------------+\r\n<\/pre>\n<p>Of course, we can also include microseconds in the initial time value:<\/p>\n<pre>SELECT TIME_FORMAT('14:35:27.123456', '%H:%i:%s.%f') AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-----------------+\r\n| Result          |\r\n+-----------------+\r\n| 14:35:27.123456 |\r\n+-----------------+\r\n<\/pre>\n<h2>Example 5 &#8211; Elapsed Time<\/h2>\n<p>The time data type is not limited to 24 hour time. It can also be used to represent elapsed time. When using this function for elapsed time, you should be aware of how the hour format specifiers work with time values outside the range 0 to 23.<\/p>\n<p>The MySQL documentation <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/date-and-time-functions.html#function_time-format\" target=\"_blank\" rel=\"noopener noreferrer\">states<\/a> the following:<\/p>\n<blockquote><p>If the time value contains an hour part that is greater than <code>23<\/code>, the <code>%H<\/code> and <code>%k<\/code> hour format specifiers produce a value larger than the usual range of <code>0..23<\/code>. The other hour format specifiers produce the hour value modulo <code>12<\/code>.<\/p><\/blockquote>\n<p>Here&#8217;s an example to demonstrate:<\/p>\n<pre>SELECT \r\n    TIME_FORMAT('24:00:00', '%H %k %h %I %l') AS '24',\r\n    TIME_FORMAT('48:00:00', '%H %k %h %I %l') AS '48',\r\n    TIME_FORMAT('100:00:00', '%H %k %h %I %l') AS '100',\r\n    TIME_FORMAT('500:00:00', '%H %k %h %I %l') AS '500';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+----------------+----------------+-----------------+-----------------+\r\n| 24             | 48             | 100             | 500             |\r\n+----------------+----------------+-----------------+-----------------+\r\n| 24 24 12 12 12 | 48 48 12 12 12 | 100 100 04 04 4 | 500 500 08 08 8 |\r\n+----------------+----------------+-----------------+-----------------+\r\n<\/pre>\n<h2>Format Specifiers<\/h2>\n<p>The following specifiers can be used to specify the return format. These are a subset of those available with the <a href=\"https:\/\/database.guide\/date_format-examples-mysql\/\"><code>DATE_FORMAT()<\/code><\/a> function.\u00a0The format value must start with a percentage sign (<code>%<\/code>).<\/p>\n<table>\n<thead>\n<tr>\n<th>Specifier<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>%f<\/code><\/td>\n<td>Microseconds (<code>000000<\/code>..<code>999999<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%H<\/code><\/td>\n<td>Hour (<code>00<\/code>..<code>23<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%h<\/code><\/td>\n<td>Hour (<code>01<\/code>..<code>12<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%I<\/code><\/td>\n<td>Hour (<code>01<\/code>..<code>12<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%i<\/code><\/td>\n<td>Minutes, numeric (<code>00<\/code>..<code>59<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%k<\/code><\/td>\n<td>Hour (<code>0<\/code>..<code>23<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%l<\/code><\/td>\n<td>Hour (<code>1<\/code>..<code>12<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%p<\/code><\/td>\n<td><code>AM<\/code> or <code>PM<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>%r<\/code><\/td>\n<td>Time, 12-hour (<code>hh:mm:ss<\/code> followed by <code>AM<\/code> or <code>PM<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%S<\/code><\/td>\n<td>Seconds (<code>00<\/code>..<code>59<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%s<\/code><\/td>\n<td>Seconds (<code>00<\/code>..<code>59<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%T<\/code><\/td>\n<td>Time, 24-hour (<code>hh:mm:ss<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>%%<\/code><\/td>\n<td>A literal <code>%<\/code> character<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, you can use the TIME_FORMAT() function to format a time value. This function works just like the DATE_FORMAT() function, except that the value can only be formatted in\u00a0hours, minutes, seconds, and microseconds. See the table at the bottom of this article for a list of format specifiers that can be used with this &#8230; <a title=\"TIME_FORMAT() Examples &#8211; MySQL\" class=\"read-more\" href=\"https:\/\/database.guide\/time_format-examples-mysql\/\" aria-label=\"Read more about TIME_FORMAT() Examples &#8211; MySQL\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[68,115,93],"class_list":["post-2871","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-date-functions","tag-dates","tag-functions"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=2871"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2871\/revisions"}],"predecessor-version":[{"id":6843,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2871\/revisions\/6843"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}