{"id":2879,"date":"2018-07-04T17:54:58","date_gmt":"2018-07-05T00:54:58","guid":{"rendered":"https:\/\/database.guide\/?p=2879"},"modified":"2021-09-05T13:20:39","modified_gmt":"2021-09-05T03:20:39","slug":"to_seconds-examples-mysql","status":"publish","type":"post","link":"https:\/\/database.guide\/to_seconds-examples-mysql\/","title":{"rendered":"TO_SECONDS() Examples &#8211; MySQL"},"content":{"rendered":"<p>In <a href=\"https:\/\/database.guide\/what-is-mysql\/\">MySQL<\/a>, the <code>TO_SECONDS()<\/code> function returns the number of seconds since the year 0.<\/p>\n<p>This function is not to be confused with the <a href=\"https:\/\/database.guide\/time_to_sec-examples-mysql\/\"><code>TIME_TO_SECONDS()<\/code><\/a> function, which returns the number of seconds in a given time value provided as an argument.<br \/>\n<!--more--><\/p>\n<h2>Syntax<\/h2>\n<p>The syntax goes like this:<\/p>\n<pre>TO_SECONDS(expr)\r\n<\/pre>\n<p>Where <code>expr<\/code> is a date or datetime value (to be compared to year 0).<\/p>\n<h2>Example 1 &#8211; Using a &#8216;date&#8217; Argument<\/h2>\n<p>Here&#8217;s an example using a date argument.<\/p>\n<pre>SELECT TO_SECONDS('2021-09-21');\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+--------------------------+\r\n| TO_SECONDS('2021-09-21') |\r\n+--------------------------+\r\n|              63799401600 |\r\n+--------------------------+\r\n<\/pre>\n<h2>Example 2 &#8211; Using a &#8216;datetime&#8217; Argument<\/h2>\n<p>Here&#8217;s an example using a datetime argument.<\/p>\n<pre>SELECT TO_SECONDS('2021-09-21 10:30:25');\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-----------------------------------+\r\n| TO_SECONDS('2021-09-21 10:30:25') |\r\n+-----------------------------------+\r\n|                       63799439425 |\r\n+-----------------------------------+\r\n<\/pre>\n<h2>Example 3 &#8211; Using the Current Date<\/h2>\n<p>In this example, I pass the <a href=\"https:\/\/database.guide\/curdate-examples-mysql\/\"><code>CURDATE()<\/code><\/a> function as the argument in order to use the current date.<\/p>\n<pre>SELECT TO_SECONDS(CURDATE()) AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------+\r\n| Result      |\r\n+-------------+\r\n| 63697968000 |\r\n+-------------+\r\n<\/pre>\n<h2>Example 4 &#8211; Using the Current Date and Time<\/h2>\n<p>In this example, I pass the <a href=\"https:\/\/database.guide\/now-examples-mysql\/\"><code>NOW()<\/code><\/a> function as the argument in order to use the current date and time.<\/p>\n<pre>SELECT TO_SECONDS(NOW()) AS 'Result';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------+\r\n| Result      |\r\n+-------------+\r\n| 63698002698 |\r\n+-------------+\r\n<\/pre>\n<h2>Two-Digit Years<\/h2>\n<p>MySQL has special rules for dealing with dates with two-digit years. Two-digit years are ambiguous because the century is unknown. Basically, the following rules apply:<\/p>\n<ul>\n<li>Year values in the range <code>00-69<\/code> are converted to <code>2000-2069<\/code>.<\/li>\n<li>Year values in the range <code>70-99<\/code> are converted to <code>1970-1999<\/code>.<\/li>\n<\/ul>\n<p>For a full explanation, see the MySQL documentation on <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/two-digit-years.html\" target=\"_blank\" rel=\"noopener noreferrer\">how MySQL deals with two-digit years<\/a>.<\/p>\n<p>Here&#8217;s an example to demonstrate:<\/p>\n<pre>SELECT \r\n  TO_SECONDS('69-10-07') AS '69 (2069)',\r\n  TO_SECONDS('70-10-07') AS '70 (1970)';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------+-------------+\r\n| 69 (2069)   | 70 (1970)   |\r\n+-------------+-------------+\r\n| 65315548800 | 62191324800 |\r\n+-------------+-------------+\r\n<\/pre>\n<h2>Abbreviated Dates<\/h2>\n<p>You can also use abbreviated dates. Here&#8217;s an example using the previous date values in abbreviated form.<\/p>\n<pre>SELECT \r\n  TO_SECONDS('691007') AS '69 (2069)',\r\n  TO_SECONDS('701007') AS '70 (1970)';\r\n<\/pre>\n<p>Result:<\/p>\n<pre>+-------------+-------------+\r\n| 69 (2069)   | 70 (1970)   |\r\n+-------------+-------------+\r\n| 65315548800 | 62191324800 |\r\n+-------------+-------------+\r\n<\/pre>\n<h2>Earlier Dates<\/h2>\n<p>The MySQL documentation <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/date-and-time-functions.html#function_to-seconds\" target=\"_blank\" rel=\"noopener noreferrer\">warns<\/a> that the <code>TO_SECONDS()<\/code> function:<\/p>\n<blockquote><p>is not intended for use with values that precede the advent of the Gregorian calendar (1582), because it does not take into account the days that were lost when the calendar was changed. For dates before 1582 (and possibly a later year in other locales), results from this function are not reliable.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>In MySQL, the TO_SECONDS() function returns the number of seconds since the year 0. This function is not to be confused with the TIME_TO_SECONDS() function, which returns the number of seconds in a given time value provided as an argument.<\/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-2879","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\/2879","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=2879"}],"version-history":[{"count":3,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2879\/revisions"}],"predecessor-version":[{"id":6842,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/2879\/revisions\/6842"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=2879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=2879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=2879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}