{"id":131,"date":"2018-11-19T23:37:47","date_gmt":"2018-11-19T15:37:47","guid":{"rendered":"http:\/\/apps.badjoerichards.com\/apps\/developerhack\/?p=131"},"modified":"2019-09-22T22:14:16","modified_gmt":"2019-09-22T14:14:16","slug":"deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time","status":"publish","type":"post","link":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/","title":{"rendered":"How to deal with time mismatch in MySQL records when date(&#8216;Y-m-d H:i:s&#8217;) in PHP does not match MySQL&#8217;s server time?"},"content":{"rendered":"<p>Try setting the locale in PHP for the current application in code.<\/p>\n<ol>\n<li>run in MYSQL : SELECT @@system_time_zone;<\/li>\n<li>Add this to the first line in PHP code,\u00a0date_default_timezone_set(&#8216;US\/Eastern&#8217;);<\/li>\n<li>Replace &#8216;US\/Eastern&#8217; in Step 2, with the timezone value you got from Step 1.<\/li>\n<li>In codeigniter, you can place Step 2 line in config.php<\/li>\n<li>If there are errors, use the right timezones here:\n<ol>\n<li>https:\/\/www.php.net\/manual\/en\/timezones.america.php<\/li>\n<li>date_default_timezone_set(&#8216;America\/New_York&#8217;); used for aspa and webhooks.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\nfunction getCurrentDatabaseTime() {\r\n    $ci =&amp; get_instance(); \r\n    $ci-&gt;load-&gt;database();\r\n\t\r\n\t$query = $ci-&gt;db-&gt;query('SELECT NOW() as `current_time`;');\r\n    \/\/ if exist\r\n    $results = array();\r\n    if ($query-&gt;num_rows() &gt; 0) {\r\n        foreach ($query-&gt;result_array() as $row)\r\n            {\r\n                return $row;\r\n            }            \r\n    }\r\n        \r\n    return FALSE; \t\r\n}\r\n\r\nfunction getCurrentDatabaseTimezone() {\r\n\t\/\/SELECT @@system_time_zone\r\n    $ci =&amp; get_instance(); \r\n    $ci-&gt;load-&gt;database();\r\n\t\r\n\t$query = $ci-&gt;db-&gt;query('SELECT @@system_time_zone as `current_time_zone`;');\r\n    \/\/ if exist\r\n    $results = array();\r\n    if ($query-&gt;num_rows() &gt; 0) {\r\n        foreach ($query-&gt;result_array() as $row)\r\n            {\r\n                return $row;\r\n            }            \r\n    }\r\n        \r\n    return FALSE;\t\r\n}\r\n\r\nfunction getPacificTime($timeString) {\r\n\/\/ Right now it's about four minutes before 1 PM, PST.\r\n$pst = new DateTimeZone('America\/Los_Angeles');\r\n\/\/$three_hours_ago = new DateTime('-3 hours', $pst); \/\/ first argument uses strtotime parsing\r\n$now = new DateTime($timeString, $pst); \/\/ first argument uses strtotime parsing\r\n\/\/echo $three_hours_ago-&gt;format('Y-m-d H:i:s'); \/\/ &quot;2010-06-15 09:56:36&quot;\r\nreturn $now-&gt;format('Y-m-d H:i:s');\t\r\n}\r\n\r\n\r\n\/\/ inside a controller , index()\r\n\r\npublic function index() {\r\n\t\t$this-&gt;load-&gt;helper('common');\r\n\t\t$current_timezone_array = getCurrentDatabaseTimezone();\r\n\t\t$current_time_array = getCurrentDatabaseTime();\r\n\t\techo 'Current Database Timezone: '.$current_timezone_array['current_time_zone'];\r\n\t\techo '&lt;hr&gt;';\r\n\t\techo 'Current Database Time: '.$current_time_array['current_time'];\r\n\t\techo '&lt;hr&gt;';\r\n\t\techo 'Current PHP Server Time: '.date('Y-m-d H:i:s');\r\n\t\techo '&lt;hr&gt;';\r\n\t\techo 'Current Pacific Time: '.getPacificTime('now');\r\n\t\tif ($current_time_array['current_time'] != date('Y-m-d H:i:s')) echo '&lt;hr&gt;&lt;font color=red&gt;Possible time issues! Database time and server time mismatch!&lt;\/font&gt;&lt;hr&gt;';\r\n\t\telse echo '&lt;hr&gt;&lt;font color=green&gt;Good! Database time and server time matches!&lt;\/font&gt;&lt;hr&gt;';\r\n\t\t\r\n\t\t\/\/ application should have mysql time and server time matching for things to work properly.\r\n\t\t\/\/ see: https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Try setting the locale in PHP for the current application in code. run in MYSQL : SELECT @@system_time_zone; Add this to the first line in PHP code,\u00a0date_default_timezone_set(&#8216;US\/Eastern&#8217;); Replace &#8216;US\/Eastern&#8217; in Step 2, with the timezone value you got from Step 1. In codeigniter, you can place Step 2 line in config.php If there are errors, &hellip; <a href=\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to deal with time mismatch in MySQL records when date(&#8216;Y-m-d H:i:s&#8217;) in PHP does not match MySQL&#8217;s server time?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to deal with time mismatch in MySQL records when date(&#039;Y-m-d H:i:s&#039;) in PHP does not match MySQL&#039;s server time? - Developer Hack<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to deal with time mismatch in MySQL records when date(&#039;Y-m-d H:i:s&#039;) in PHP does not match MySQL&#039;s server time? - Developer Hack\" \/>\n<meta property=\"og:description\" content=\"Try setting the locale in PHP for the current application in code. run in MYSQL : SELECT @@system_time_zone; Add this to the first line in PHP code,\u00a0date_default_timezone_set(&#8216;US\/Eastern&#8217;); Replace &#8216;US\/Eastern&#8217; in Step 2, with the timezone value you got from Step 1. In codeigniter, you can place Step 2 line in config.php If there are errors, &hellip; Continue reading &quot;How to deal with time mismatch in MySQL records when date(&#8216;Y-m-d H:i:s&#8217;) in PHP does not match MySQL&#8217;s server time?&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\" \/>\n<meta property=\"og:site_name\" content=\"Developer Hack\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-19T15:37:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-22T14:14:16+00:00\" \/>\n<meta name=\"author\" content=\"Bad Joe\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bad Joe\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website\",\"url\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/\",\"name\":\"Developer Hack\",\"description\":\"Hacks, tips, tricks and best practices for developers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\",\"url\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\",\"name\":\"How to deal with time mismatch in MySQL records when date('Y-m-d H:i:s') in PHP does not match MySQL's server time? - Developer Hack\",\"isPartOf\":{\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website\"},\"datePublished\":\"2018-11-19T15:37:47+00:00\",\"dateModified\":\"2019-09-22T14:14:16+00:00\",\"author\":{\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6\"},\"breadcrumb\":{\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to deal with time mismatch in MySQL records when date(&#8216;Y-m-d H:i:s&#8217;) in PHP does not match MySQL&#8217;s server time?\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6\",\"name\":\"Bad Joe\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g\",\"caption\":\"Bad Joe\"},\"url\":\"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to deal with time mismatch in MySQL records when date('Y-m-d H:i:s') in PHP does not match MySQL's server time? - Developer Hack","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:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/","og_locale":"en_US","og_type":"article","og_title":"How to deal with time mismatch in MySQL records when date('Y-m-d H:i:s') in PHP does not match MySQL's server time? - Developer Hack","og_description":"Try setting the locale in PHP for the current application in code. run in MYSQL : SELECT @@system_time_zone; Add this to the first line in PHP code,\u00a0date_default_timezone_set(&#8216;US\/Eastern&#8217;); Replace &#8216;US\/Eastern&#8217; in Step 2, with the timezone value you got from Step 1. In codeigniter, you can place Step 2 line in config.php If there are errors, &hellip; Continue reading \"How to deal with time mismatch in MySQL records when date(&#8216;Y-m-d H:i:s&#8217;) in PHP does not match MySQL&#8217;s server time?\"","og_url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/","og_site_name":"Developer Hack","article_published_time":"2018-11-19T15:37:47+00:00","article_modified_time":"2019-09-22T14:14:16+00:00","author":"Bad Joe","twitter_card":"summary","twitter_misc":{"Written by":"Bad Joe","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website","url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/","name":"Developer Hack","description":"Hacks, tips, tricks and best practices for developers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/","url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/","name":"How to deal with time mismatch in MySQL records when date('Y-m-d H:i:s') in PHP does not match MySQL's server time? - Developer Hack","isPartOf":{"@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#website"},"datePublished":"2018-11-19T15:37:47+00:00","dateModified":"2019-09-22T14:14:16+00:00","author":{"@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6"},"breadcrumb":{"@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/deal-time-mismtach-mysql-records-datey-m-d-php-not-match-mysqls-server-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/"},{"@type":"ListItem","position":2,"name":"How to deal with time mismatch in MySQL records when date(&#8216;Y-m-d H:i:s&#8217;) in PHP does not match MySQL&#8217;s server time?"}]},{"@type":"Person","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/01df012e94dd976ac49e50cdbf0e38d6","name":"Bad Joe","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5d413dbb6a01d82a56f2ceb6a82e8704?s=96&d=mm&r=g","caption":"Bad Joe"},"url":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts\/131"}],"collection":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":12,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/posts\/131\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/apps.badjoerichards.com\/apps\/developerhack\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}