{"id":4050,"date":"2015-11-04T23:52:34","date_gmt":"2015-11-05T07:52:34","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=4050"},"modified":"2023-12-27T17:54:24","modified_gmt":"2023-12-28T00:54:24","slug":"mysql-datetime","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-datetime\/","title":{"rendered":"MySQL DATETIME Data Type"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about MySQL <code>DATETIME<\/code> data type and how to use some handy functions for manipulating&nbsp;<code>DATETIME<\/code>&nbsp;effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL DATETIME data type<\/h2>\n\n\n\n<p>MySQL <code>DATETIME<\/code> data type allows you to store a value that contains both <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-date\/\">date<\/a> and <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-time\/\">time<\/a>. <\/p>\n\n\n\n<p>When you\u00a0<a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query data<\/a> from a <code>DATETIME<\/code> column, MySQL displays the <code>DATETIME<\/code> value in the following format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">'YYYY-MM-DD HH:MM:SS'<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you insert a value into a <code>DATETIME<\/code> column, you use the same format. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">INSERT INTO table_name(datetime_column)\nVALUES(<span class=\"hljs-string\">'2023-12-31 15:30:45'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To populate a column with the current date and time, you use the result of the <code>CURRENT_TIMESTAMP<\/code> or <code>NOW()<\/code> function as the default value. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">CREATE TABLE events(\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    event_name VARCHAR(<span class=\"hljs-number\">255<\/span>) NOT <span class=\"hljs-keyword\">NULL<\/span>,\n    started_at DATETIME NOT <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> CURRENT_TIMESTAMP\n);\n\nINSERT INTO events(event_name)\nVALUES(<span class=\"hljs-string\">'Connected to MySQL Server'<\/span>);\n\nSELECT * FROM events;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+---------------------------+---------------------+\r\n| id | event_name                | started_at          |\r\n+----+---------------------------+---------------------+\r\n|  <span class=\"hljs-number\">1<\/span> | Connected to MySQL Server | <span class=\"hljs-number\">2023<\/span><span class=\"hljs-number\">-12<\/span><span class=\"hljs-number\">-28<\/span> <span class=\"hljs-number\">07<\/span>:<span class=\"hljs-number\">51<\/span>:<span class=\"hljs-number\">18<\/span> |\r\n+----+---------------------------+---------------------+\r\n<span class=\"hljs-number\">1<\/span> row <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By default, <code>DATETIME<\/code>\u00a0values range from <code>1000-01-01 00:00:00<\/code> to <code>9999-12-31 23:59:59<\/code>. MySQL uses 5 bytes to store a <code>DATETIME<\/code> value.<\/p>\n\n\n\n<p>In addition, a <code>DATETIME<\/code> value can include a trailing fractional second up to microseconds with the format\u00a0<code>YYYY-MM-DD HH:MM:SS[.fraction]<\/code> e.g., <code>2015-12-20 10:01:00.999999<\/code>. <\/p>\n\n\n\n<p>When including the fractional second precision, <code>DATETIME<\/code> values require more storage as illustrated in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Fractional Seconds Precision<\/strong><\/th><th><strong>Storage (Bytes)<\/strong><\/th><\/tr><tr><td>0<\/td><td>0<\/td><\/tr><tr><td>1, 2<\/td><td>1<\/td><\/tr><tr><td>3, 4<\/td><td>2<\/td><\/tr><tr><td>5, 6<\/td><td>3<\/td><\/tr><\/thead><\/table><\/figure>\n\n\n\n<p>For example, <code>2015-12-20 10:01:00.999999 <\/code>requires 8 bytes, 5 \u00a0bytes for <code>2015-12-20 10:01:00<\/code>\u00a0and 3 bytes for <code>.999999<\/code>\u00a0while <code>2015-12-20 10:01:00.9<\/code> \u00a0requires only\u00a06 bytes, 1 byte for the fractional second precision.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL DATETIME vs.TIMESTAMP<\/h2>\n\n\n\n<p>MySQL provides another temporal data type that is similar to the <code>DATETIME<\/code> called &nbsp;<code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/understanding-mysql-timestamp\/\">TIMESTAMP<\/a><\/code>.<\/p>\n\n\n\n<p>The <code>TIMESTAMP<\/code> requires 4 bytes while <code>DATETIME<\/code> requires 5 bytes. Both <code>TIMESTAMP<\/code> and <code>DATETIME <\/code>require additional bytes for fractional seconds precision.<\/p>\n\n\n\n<p><code>TIMESTAMP<\/code> values range&nbsp;from <code>1970-01-01 00:00:01 UTC<\/code> to <code>2038-01-19 03:14:07 UTC<\/code>. If you want to store temporal values&nbsp;that are beyond 2038, you should use <code>DATETIME<\/code> instead of <code>TIMESTAMP<\/code>.<\/p>\n\n\n\n<p>MySQL stores <code>TIMESTAMP<\/code> in UTC value.&nbsp;However,&nbsp;MySQL&nbsp;stores the <code>DATETIME<\/code> value as is without timezone. Let&#8217;s see the following example.<\/p>\n\n\n\n<p>First, set the timezone of the&nbsp;current connection&nbsp;to <code>+00:00<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">time_zone<\/span> = <span class=\"hljs-string\">'+00:00'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> named <code>timestamp_n_datetime<\/code> that consists of two columns: <code>ts<\/code> and <code>dt<\/code> with <code>TIMESTAMP<\/code> and <code>DATETIME<\/code> types using the following statement.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> timestamp_n_datetime (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    ts <span class=\"hljs-built_in\">TIMESTAMP<\/span>,\n    dt DATETIME\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert<\/a> the current date and time into both <code>ts<\/code> and <code>dt<\/code>&nbsp;columns of the&nbsp;<code>timestamp_n_datetime<\/code> table,<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> timestamp_n_datetime(ts,dt)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-keyword\">NOW<\/span>(),<span class=\"hljs-keyword\">NOW<\/span>());<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After that, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query data<\/a> from the <code>timestamp_n_datetime<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    ts, \n    dt\n<span class=\"hljs-keyword\">FROM<\/span>\n    timestamp_n_datetime;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"267\" height=\"45\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP.jpg\" alt=\"MySQL DATETIME vs TIMESTAMP\" class=\"wp-image-4051\" title=\"MySQL DATETIME vs TIMESTAMP\"\/><\/figure>\n\n\n\n<p>Both values in <code>DATETIME<\/code> and <code>TIMESTAMP<\/code> columns are the same.<\/p>\n\n\n\n<p>Finally, set the connection&#8217;s time zone to <code>+03:00<\/code> and query data from the <code>timestamp_n_datetime<\/code> table again.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">time_zone<\/span> = <span class=\"hljs-string\">'+03:00'<\/span>;\n\n<span class=\"hljs-keyword\">SELECT<\/span> \n    ts, \n    dt\n<span class=\"hljs-keyword\">FROM<\/span>\n    timestamp_n_datetime;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"269\" height=\"46\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP-timezone-changes.jpg\" alt=\"MySQL DATETIME vs TIMESTAMP timezone changes\" class=\"wp-image-4052\" title=\"MySQL DATETIME vs TIMESTAMP timezone changes\"\/><\/figure>\n\n\n\n<p>The output indicates that the value in the <code>TIMESTAMP<\/code> column is different. This is because the\u00a0<code>TIMESTAMP<\/code> column stores the date and time value in UTC when we change the time zone, the value of the <code>TIMESTAMP<\/code> column is adjusted according to the new time zone.<\/p>\n\n\n\n<p>It means that if you use the <code>TIMESTAMP<\/code> data to store date and time values, you should take serious consideration when you move your database to a server located in a different time zone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL DATETIME functions<\/h2>\n\n\n\n<p>The following statement sets the variable <code>@dt<\/code> to the current date and time using the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-now-function\/\">NOW()<\/a><\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SET<\/span> @dt =  <span class=\"hljs-keyword\">NOW<\/span>();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To query the value of the <code>@dt<\/code> variable, you use the following&nbsp;<code>SELECT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> @dt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"143\" height=\"35\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-NOW-function.png\" alt=\"MySQL DATETIME - NOW function\" class=\"wp-image-6118\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL DATE() function<\/h3>\n\n\n\n<p>To extract the date portion from a <code>DATETIME<\/code> value, you use the <code>DATE<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-built_in\">DATE<\/span>(@dt);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"98\" height=\"36\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE-function-example.png\" alt=\"MySQL DATETIME - DATE function example\" class=\"wp-image-6119\"\/><\/figure>\n\n\n\n<p>This function is very useful in case you want to query data based on a date but the data stored in the column is based on both date and time.<\/p>\n\n\n\n<p>Let&#8217;s see the following example.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> test_dt (\n    <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    created_at DATETIME\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> test_dt(created_at)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'2015-11-05 14:29:36'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Suppose you want to know which row created on <code>2015-11-05<\/code>, you use the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    test_dt\n<span class=\"hljs-keyword\">WHERE<\/span>\n    created_at = <span class=\"hljs-string\">'2015-11-05'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns no rows.<\/p>\n\n\n\n<p>This is because the <code>created_at<\/code> column contains not only the date but also the time. To correct it, you use the <code>DATE<\/code> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    test_dt\n<span class=\"hljs-keyword\">WHERE<\/span>\n    <span class=\"hljs-built_in\">DATE<\/span>(created_at) = <span class=\"hljs-string\">'2015-11-05'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"182\" height=\"40\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE-function.png\" alt=\"MySQL DATETIME - DATE function\" class=\"wp-image-6117\"\/><\/figure>\n\n\n\n<p>It returns one row as expected. In case the table has many rows, MySQL has to perform a full table scan to locate the&nbsp;rows&nbsp;that match the condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL TIME function<\/h3>\n\n\n\n<p>To extract the time portion from a <code>DATETIME<\/code> value, you use the <code>TIME<\/code> function as the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-built_in\">TIME<\/span>(@dt);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"92\" height=\"38\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-TIME-function.png\" alt=\"MySQL DATETIME - TIME function\" class=\"wp-image-6120\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE and SECOND functions<\/h3>\n\n\n\n<p>To get the year, quarter, month, week, day, hour, minute, and second from a <code>DATETIME<\/code> value, you use the functions as shown in the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    <span class=\"hljs-keyword\">HOUR<\/span>(@dt),\n    <span class=\"hljs-keyword\">MINUTE<\/span>(@dt),\n    <span class=\"hljs-keyword\">SECOND<\/span>(@dt),\n    <span class=\"hljs-keyword\">DAY<\/span>(@dt),\n    <span class=\"hljs-keyword\">WEEK<\/span>(@dt),\n    <span class=\"hljs-keyword\">MONTH<\/span>(@dt),\n    <span class=\"hljs-keyword\">QUARTER<\/span>(@dt),\n    <span class=\"hljs-keyword\">YEAR<\/span>(@dt);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"647\" height=\"41\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-datetime-functions.png\" alt=\"MySQL DATETIME - datetime functions\" class=\"wp-image-6121\" title=\"MySQL DATETIME - datetime functions\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-datetime-functions.png 647w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-datetime-functions-300x19.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL DATE_FORMAT function<\/h3>\n\n\n\n<p>To format a <code>DATETIME<\/code> value, you use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-date_format\/\">DATE_FORMAT<\/a><\/code> function. For example, the following statement formats a <code>DATETIME<\/code> value based on the <code>%H:%i:%s - %W %M %Y<\/code>&nbsp;format:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">DATE_FORMAT<\/span>(@dt, <span class=\"hljs-string\">'%H:%i:%s - %W %M %Y'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"285\" height=\"37\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_FORMAT-function.png\" alt=\"MySQL DATETIME - DATE_FORMAT function\" class=\"wp-image-6122\" title=\"MySQL DATETIME - DATE_FORMAT function\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL DATE_ADD function<\/h3>\n\n\n\n<p>To add an <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-interval\/\">interval <\/a>to a <code>DATETIME<\/code> value, you use <a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-date_add\/\"><code>DATE_ADD<\/code><\/a> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> @dt <span class=\"hljs-keyword\">start<\/span>, \n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">SECOND<\/span>) <span class=\"hljs-string\">'1 second later'<\/span>,\n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">MINUTE<\/span>) <span class=\"hljs-string\">'1 minute later'<\/span>,\n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">HOUR<\/span>) <span class=\"hljs-string\">'1 hour later'<\/span>,\n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">DAY<\/span>) <span class=\"hljs-string\">'1 day later'<\/span>,\n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">WEEK<\/span>) <span class=\"hljs-string\">'1 week later'<\/span>,\n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">MONTH<\/span>) <span class=\"hljs-string\">'1 month later'<\/span>,\n       <span class=\"hljs-keyword\">DATE_ADD<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">YEAR<\/span>) <span class=\"hljs-string\">'1 year later'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"976\" height=\"35\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_ADD-function.png\" alt=\"MySQL DATETIME - DATE_ADD function\" class=\"wp-image-6123\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_ADD-function.png 976w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_ADD-function-300x11.png 300w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_ADD-function-768x28.png 768w\" sizes=\"auto, (max-width: 976px) 100vw, 976px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL DATE_SUB function<\/h3>\n\n\n\n<p>To subtract an interval from a <code>DATETIME<\/code> value, you use <a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-date_sub\/\"><code>DATE_SUB<\/code><\/a> function as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> @dt <span class=\"hljs-keyword\">start<\/span>, \n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">SECOND<\/span>) <span class=\"hljs-string\">'1 second before'<\/span>,\n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">MINUTE<\/span>) <span class=\"hljs-string\">'1 minute before'<\/span>,\n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">HOUR<\/span>) <span class=\"hljs-string\">'1 hour before'<\/span>,\n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">DAY<\/span>) <span class=\"hljs-string\">'1 day before'<\/span>,\n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">WEEK<\/span>) <span class=\"hljs-string\">'1 week before'<\/span>,\n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">MONTH<\/span>) <span class=\"hljs-string\">'1 month before'<\/span>,\n       <span class=\"hljs-keyword\">DATE_SUB<\/span>(@dt, <span class=\"hljs-built_in\">INTERVAL<\/span> <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">YEAR<\/span>) <span class=\"hljs-string\">'1 year before'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"980\" height=\"41\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_SUB-function.png\" alt=\"MySQL DATETIME - DATE_SUB function\" class=\"wp-image-6124\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_SUB-function.png 980w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_SUB-function-300x13.png 300w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATE_SUB-function-768x32.png 768w\" sizes=\"auto, (max-width: 980px) 100vw, 980px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL DATE_DIFF function<\/h3>\n\n\n\n<p>To calculate a difference in days between two <code>DATETIME<\/code> values, you use the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-date-functions\/mysql-datediff-function\/\">DATEDIFF<\/a><\/code> function. Notice that the <code>DATEDIFF<\/code> function only considers the date part of a <code>DATETIME<\/code> value in the calculation.<\/p>\n\n\n\n<p>See the following example.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> named <code>datediff_test<\/code> that has one column whose data type is <code>DATETIME<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> datediff_test (\n    dt DATETIME\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, insert some rows into the <code>datediff_test<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> datediff_test(dt)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'2010-04-30 07:27:39'<\/span>),\n\t(<span class=\"hljs-string\">'2010-05-17 22:52:21'<\/span>),\n\t(<span class=\"hljs-string\">'2010-05-18 01:19:10'<\/span>),\n\t(<span class=\"hljs-string\">'2010-05-22 14:17:16'<\/span>),\n\t(<span class=\"hljs-string\">'2010-05-26 03:26:56'<\/span>),\n\t(<span class=\"hljs-string\">'2010-06-10 04:44:38'<\/span>),\n\t(<span class=\"hljs-string\">'2010-06-13 13:55:53'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, use the <code>DATEDIFF<\/code> function to compare the current date and time with the value in each row of the <code>datediff_test<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    dt, \n    <span class=\"hljs-keyword\">DATEDIFF<\/span>(<span class=\"hljs-keyword\">NOW<\/span>(), dt)\n<span class=\"hljs-keyword\">FROM<\/span>\n    datediff_test;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"158\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-DATEDIFF-Example.png\" alt=\"MySQL DATETIME - DATEDIFF Example\" class=\"wp-image-6116\" title=\"MySQL DATETIME - DATEDIFF Example\"\/><\/figure>\n\n\n\n<p>In this tutorial,&nbsp;you have learned about MySQL <code>DATETIME<\/code> data type and some useful <code>DATETIME<\/code> functions.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4050\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-datetime\/\"\n\t\t\t\tdata-post-title=\"MySQL DATETIME Data Type\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4050\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-datetime\/\"\n\t\t\t\tdata-post-title=\"MySQL DATETIME Data Type\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":71,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4050","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MySQL DATETIME Data Type<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values effectively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-datetime\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL DATETIME Data Type\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values effectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-datetime\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-28T00:54:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/\",\"name\":\"MySQL DATETIME Data Type\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2015\\\/11\\\/MySQL-DATETIME-vs-TIMESTAMP.jpg\",\"datePublished\":\"2015-11-05T07:52:34+00:00\",\"dateModified\":\"2023-12-28T00:54:24+00:00\",\"description\":\"In this tutorial, you will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values effectively.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2015\\\/11\\\/MySQL-DATETIME-vs-TIMESTAMP.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2015\\\/11\\\/MySQL-DATETIME-vs-TIMESTAMP.jpg\",\"width\":267,\"height\":45},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-datetime\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL DATETIME Data Type\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL DATETIME Data Type","description":"In this tutorial, you will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values effectively.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/","og_locale":"en_US","og_type":"article","og_title":"MySQL DATETIME Data Type","og_description":"In this tutorial, you will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values effectively.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-28T00:54:24+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/","url":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/","name":"MySQL DATETIME Data Type","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP.jpg","datePublished":"2015-11-05T07:52:34+00:00","dateModified":"2023-12-28T00:54:24+00:00","description":"In this tutorial, you will learn about MySQL DATETIME data type and some useful functions to manipulate DATETIME values effectively.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-datetime\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP.jpg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2015\/11\/MySQL-DATETIME-vs-TIMESTAMP.jpg","width":267,"height":45},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-datetime\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL DATETIME Data Type"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/4050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=4050"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/4050\/revisions"}],"predecessor-version":[{"id":13588,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/4050\/revisions\/13588"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=4050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}