{"id":2876,"date":"2018-07-04T16:09:39","date_gmt":"2018-07-04T23:09:39","guid":{"rendered":"https:\/\/database.guide\/?p=2876"},"modified":"2021-09-05T13:20:39","modified_gmt":"2021-09-05T03:20:39","slug":"time_to_sec-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/time_to_sec-examples-mysql\/","title":{"rendered":"TIME_TO_SEC() Examples &#8211; MySQL"},"content":{"rendered":"<p>When using <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, you can use the <code>TIME_TO_SEC()<\/code> function to return the number of seconds in a time value. Specifically, this function returns the time argument, converted to seconds.<\/p>\n<p>This function is not to be confused with the <a href=\"https:\/\/database.guide\/to_seconds-examples-mysql\/\"><code>TO_SECONDS()<\/code><\/a> function, which, given a date or datetime argument, returns the number of seconds since year 0.<\/p>\n<p>Here&#8217;s how <code>TIME_TO_SEC()<\/code> works.<\/p>\n<p><!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>TIME_TO_SEC(time)\r\n<\/pre>\n<p>Where <code>time<\/code> is the time value you want converted to seconds.<\/p>\n<h2>Example 1 &#8211; Basic Example<\/h2>\n<p>Here&#8217;s an example to demonstrate.<\/p>\n<pre>SELECT TIME_TO_SEC('00:01:00');\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------------------+\r\n| TIME_TO_SEC('00:01:00') |\r\n+-------------------------+\r\n|                      60 |\r\n+-------------------------+\r\n<\/pre>\n<h2>Example 2 &#8211; Larger Value<\/h2>\n<p>And here&#8217;s what it looks like when we use a larger time value:<\/p>\n<pre>SELECT TIME_TO_SEC('01:00:00');\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------------------+\r\n| TIME_TO_SEC('01:00:00') |\r\n+-------------------------+\r\n|                    3600 |\r\n+-------------------------+\r\n<\/pre>\n<h2>Example 3 &#8211; Current Time<\/h2>\n<p>You can pass the <a href=\"https:\/\/database.guide\/curtime-examples-mysql\/\"><code>CURTIME()<\/code><\/a> function as an argument to return the number of seconds in the current time.<\/p>\n<pre>SELECT \r\n  CURTIME() AS 'Current Time',\r\n  TIME_TO_SEC(CURTIME()) AS 'Seconds';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+--------------+---------+\r\n| Current Time | Seconds |\r\n+--------------+---------+\r\n| 09:04:47     |   32687 |\r\n+--------------+---------+\r\n<\/pre>\n<p>Here&#8217;s the same example, but using the <a href=\"https:\/\/database.guide\/current_time-examples-mysql\/\"><code>CURRENT_TIME()<\/code><\/a> function (which is a synonym for <code>CURTIME()<\/code>).<\/p>\n<pre>SELECT \r\n  CURRENT_TIME() AS 'Current Time',\r\n  TIME_TO_SEC(CURRENT_TIME()) AS 'Seconds';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+--------------+---------+\r\n| Current Time | Seconds |\r\n+--------------+---------+\r\n| 09:05:23     |   32723 |\r\n+--------------+---------+\r\n<\/pre>\n<h2>Example 4 &#8211; Elapsed Time<\/h2>\n<p>The time data type isn&#8217;t limited to just time of the day. It can also be used to represent elapsed time.<\/p>\n<p>Here&#8217;s an example of getting the number of seconds in 400 hours.<\/p>\n<pre>SELECT TIME_TO_SEC('400:00:00');\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+--------------------------+\r\n| TIME_TO_SEC('400:00:00') |\r\n+--------------------------+\r\n|                  1440000 |\r\n+--------------------------+\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using MySQL, you can use the TIME_TO_SEC() function to return the number of seconds in a time value. Specifically, this function returns the time argument, converted to seconds. This function is not to be confused with the TO_SECONDS() function, which, given a date or datetime argument, returns the number of seconds since year 0. &#8230; <a title=\"TIME_TO_SEC() Examples &#8211; MySQL\" class=\"read-more\" href=\"https:\/\/database.guide\/time_to_sec-examples-mysql\/\" aria-label=\"Read more about TIME_TO_SEC() 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":[72,68,115,93],"class_list":["post-2876","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-conversion-functions","tag-date-functions","tag-dates","tag-functions"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2876","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=2876"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2876\/revisions"}],"predecessor-version":[{"id":2883,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2876\/revisions\/2883"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}