{"id":29135,"date":"2024-05-04T19:25:57","date_gmt":"2024-05-04T18:25:57","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/insertando-valores-datetime-en-mysql-guia-practica\/"},"modified":"2024-06-03T17:44:39","modified_gmt":"2024-06-03T16:44:39","slug":"insertando-valores-datetime-en-mysql-guia-practica","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/","title":{"rendered":"Inserting DateTime Values in MySQL: Practical Guide"},"content":{"rendered":"<p>Working with dates and times is a fundamental aspect of database management. MySQL, one of the most popular database management systems, offers several ways to insert and handle datetime values. In this post, I&#039;ll show you how you can insert datetime values into a MySQL DATETIME column efficiently, ensuring that your data is stored correctly and ready for any subsequent queries or applications.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#%C2%BFQue_es_el_Tipo_de_Datos_DATETIME\" >What is the DATETIME Data Type?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#Formato_Estandar_de_DATETIME_en_MySQL\" >Standard DATETIME Format in MySQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#Insertando_Datos_Estaticos_en_DATETIME\" >Inserting Static Data into DATETIME<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#Insertando_Fechas_y_Horas_Actuales\" >Inserting Current Dates and Times<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#Uso_de_Funciones_para_Modificar_DATETIME_Durante_la_Insercion\" >Using Functions to Modify DATETIME During Insertion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#Consideraciones_de_Precision_y_Zonas_Horarias\" >Precision Considerations and Time Zones<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/inserting-datetime-values-in-mysql-practical-guide\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_el_Tipo_de_Datos_DATETIME\"><\/span>What is the DATETIME Data Type?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In MySQL, the DATETIME data type is used to store combinations of dates and times, including year, month, day, hour, minute, and second. This type of data is ideal for records that require exact date and time stamping, such as activity logs, transaction logs, or journal entries.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Formato_Estandar_de_DATETIME_en_MySQL\"><\/span>Standard DATETIME Format in MySQL<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The standard format for entering a value of type DATETIME in MySQL is <code>YYYY-MM-DD HH:MM:SS<\/code>. It is crucial to follow this format when manually inserting values into a database to avoid insertion errors and compatibility issues with other functions or applications that process this data later.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Insertando_Datos_Estaticos_en_DATETIME\"><\/span>Inserting Static Data into DATETIME<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Suppose you have a table called <code>Events<\/code> which stores information about different events organized by a company, including the date and time of the event. The table could be defined as follows:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">CREATE TABLE Events ( ID int NOT NULL AUTO_INCREMENT PRIMARY KEY, EventName varchar(255), DateTime DATETIME );<\/code><\/pre>\n<p>If you want to insert a specific event into this table, you can do so by directly specifying the date and time of the event. For example, to add an event that occurred on January 1, 2023 at 15:30:00, you would use the following SQL command:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">INSERT INTO Events (EventName, DateTime) VALUES (&#039;Product Release&#039;, &#039;2023-01-01 15:30:00&#039;);<\/code><\/pre>\n<p>This method is direct and works very well when you know the exact values you want to insert.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Insertando_Fechas_y_Horas_Actuales\"><\/span>Inserting Current Dates and Times<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Very often, it is necessary to record the exact moment when an event occurs or a record is inserted. MySQL makes this easy with functions like <code>NOW()<\/code>, which returns the current date and time of the system where the database server is running.<\/p>\n<p>If you want to register in the table <code>Events<\/code> the exact moment a new event is added you can do the following:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">INSERT INTO Events (EventName, DateTime) VALUES (&#039;Strategy Meeting&#039;, NOW());<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Uso_de_Funciones_para_Modificar_DATETIME_Durante_la_Insercion\"><\/span>Using Functions to Modify DATETIME During Insertion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>One of the great advantages of MySQL is its ability to manipulate date and time data at the time of insertion. For example, if you want to set an event that will be exactly one week after the current date, you could use the function <code>DATE_ADD()<\/code>:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">INSERT INTO Events (EventName, DateTime) VALUES (&#039;Webinar&#039;, DATE_ADD(NOW(), INTERVAL 1 WEEK));<\/code><\/pre>\n<p>Similarly, if you need to adjust the time of an event to reflect a different time zone or to fix an error, you can use <code>DATE_SUB()<\/code> to subtract an interval from the DATETIME:<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">INSERT INTO Events (EventName, DateTime) VALUES (&#039;International Conference&#039;, DATE_ADD(&#039;2023-02-01 09:00:00&#039;, INTERVAL -5 HOUR));<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Consideraciones_de_Precision_y_Zonas_Horarias\"><\/span>Precision Considerations and Time Zones<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When working with DATETIME, it is essential to consider the time zone in which the values are interpreted. If your application is used in multiple time zones, you can store DateTimes in Coordinated Universal Time (UTC) and convert them to the user&#039;s local time zone when necessary.<\/p>\n<p>It is possible to configure the MySQL server time zone so that the results of functions like <code>NOW()<\/code> reflect local time. This can be done by adjusting server settings or at the session level.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Correctly handling datetime values in MySQL is essential for developing robust and reliable applications. Directly use static values, take advantage of functions like <code>NOW()<\/code> to insert the current moment, or manipulate dates and times with <code>DATE_ADD()<\/code> y <code>DATE_SUB()<\/code> They are crucial techniques that every developer should know.<\/p>\n<p>I hope this guide has been useful for your development projects with MySQL. If you have any questions or comments, do not hesitate to visit <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">my contact page<\/a>.<\/p>\n<p>Want more tutorials and tips on databases and programming? Continue exploring my site <a href=\"https:\/\/nelkodev.com\/en\/\">NelkoDev<\/a> for more resources and practical guides.<\/p>","protected":false},"excerpt":{"rendered":"<p>Working with dates and times is a fundamental aspect of database management. MySQL, one of the most popular database management systems, offers several ways to insert and manage datetime values. In this post, I will show you how you can insert datetime values into a MySQL DATETIME column in a simple way.<\/p>","protected":false},"author":1,"featured_media":29136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2185,2021],"tags":[205,2189,639,2125,358,2159,2190,352,1214,924],"class_list":["post-29135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-curso-mysql","category-modifying-data","tag-blog","tag-curso","tag-data","tag-datetime","tag-guia","tag-insertando","tag-modifying","tag-mysql","tag-practica","tag-valores"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=29135"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29135\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/29136"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=29135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=29135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=29135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}