{"id":975,"date":"2020-07-05T13:56:05","date_gmt":"2020-07-05T11:56:05","guid":{"rendered":"https:\/\/ekiwi.de\/?p=975"},"modified":"2020-07-05T13:56:12","modified_gmt":"2020-07-05T11:56:12","slug":"php-datum-und-zeit-in-mysql-datenbank-einfuegen","status":"publish","type":"post","link":"https:\/\/ekiwi.de\/index.php\/975\/php-datum-und-zeit-in-mysql-datenbank-einfuegen\/","title":{"rendered":"PHP: Datum und Zeit in MySQL-Datenbank einf\u00fcgen"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Inhalt<\/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: #999;color:#999\" 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: #999;color:#999\" 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:\/\/ekiwi.de\/index.php\/975\/php-datum-und-zeit-in-mysql-datenbank-einfuegen\/#Die_Datenbank\" >Die Datenbank<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi.de\/index.php\/975\/php-datum-und-zeit-in-mysql-datenbank-einfuegen\/#Einfuegen_von_Datum_und_Zeit_mit_PHP\" >Einf\u00fcgen von Datum und Zeit mit PHP<\/a><\/li><\/ul><\/nav><\/div>\n<p>Wie kann ich die aktuelle Zeit in eine MySQL-Datenbank einf\u00fcgen?<\/p>\n<p><!--more--><\/p>\n<p>In diesem Beispiel wollen wir in eine MySQL-Datenbank die aktuelle Zeit und das aktuelle Datum einf\u00fcgen. Z.B. um die Registrierung eines Benutzers zeitlich nachvollziehen zu k\u00f6nnen.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Die_Datenbank\"><\/span>Die Datenbank<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Unsere Tabelle im Beispiel sieht wie folgt aus:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2020\/07\/mysql_1.png\" alt=\"\" width=\"491\" height=\"151\" class=\"alignnone size-full wp-image-976\" srcset=\"\/wp-content\/uploads\/2020\/07\/mysql_1.png 491w, \/wp-content\/uploads\/2020\/07\/mysql_1-300x92.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/p>\n<p>Datum und Uhrzeit sollen in das Feld &#8222;registered&#8220; eingetragen werden, welches wir als &#8222;datetime&#8220; festlegen.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Einfuegen_von_Datum_und_Zeit_mit_PHP\"><\/span>Einf\u00fcgen von Datum und Zeit mit PHP<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Mit folgendem Code f\u00fcgen wir nun einen neuen Benutzer ein und speichern zugleich das aktuelle Datum und Uhrzeit.<\/p>\n<pre>\r\n&lt;?php\r\n$link = mysqli_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;, &quot;datum&quot;);\r\n\r\nif (!$link)\r\n{\r\n    echo &quot;Fehler: konnte nicht mit MySQL verbinden.&quot; . PHP_EOL;\r\n    exit;\r\n}\r\n\r\n$sql = 'INSERT INTO benutzer (name, email, registered) VALUES (&quot;Andy&quot;, &quot;andy.dunkel@ekiwi.de&quot;, now())';\r\nmysqli_query($link, $sql);\r\nmysqli_close($link);\r\necho &quot;Datensatz eingef\u00fcgt&quot;;\r\n?&gt;\r\n<\/pre>\n<p>Die aktuelle Zeit und Datum ermitteln wir automatisch in unserem SQL-Statement:<\/p>\n<pre>\r\nINSERT INTO benutzer (name, email, registered) VALUES (&quot;Andy&quot;, &quot;andy.dunkel@ekiwi.de&quot;, now());\r\n<\/pre>\n<p>Hier kommt die &#8222;now()&#8220; Funktion zum Einsatz, welche das Datetime-Feld automatisch mit aktueller Uhrzeit und Datum bef\u00fcllt.<\/p>\n<p>Das Ergebnis:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/wp-content\/uploads\/2020\/07\/mysql_2.png\" alt=\"\" width=\"649\" height=\"66\" class=\"alignnone size-full wp-image-977\" srcset=\"\/wp-content\/uploads\/2020\/07\/mysql_2.png 649w, \/wp-content\/uploads\/2020\/07\/mysql_2-300x31.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>","protected":false},"excerpt":{"rendered":"<p>Wie kann ich die aktuelle Zeit in eine MySQL-Datenbank einf\u00fcgen?<\/p>\n","protected":false},"author":1,"featured_media":583,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[14],"tags":[24,31],"class_list":["post-975","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-mysql","tag-php"],"_links":{"self":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts\/975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/comments?post=975"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/posts\/975\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/media\/583"}],"wp:attachment":[{"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/media?parent=975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/categories?post=975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi.de\/index.php\/wp-json\/wp\/v2\/tags?post=975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}