{"id":724,"date":"2005-03-14T01:02:44","date_gmt":"2005-03-13T16:02:44","guid":{"rendered":"http:\/\/unknowngenius.com\/blog\/archives\/2005\/03\/14\/mysql-php-and-time-conversion\/"},"modified":"2005-07-23T17:29:25","modified_gmt":"2005-07-23T08:29:25","slug":"mysql-php-and-time-conversion","status":"publish","type":"post","link":"https:\/\/unknowngenius.com\/blog\/archives\/2005\/03\/14\/mysql-php-and-time-conversion\/","title":{"rendered":"MySQL, PHP and time conversion"},"content":{"rendered":"<p>Another post of utter geekery with some highly uninteresting and very unnecessary programming tidbit (I bet you can&#8217;t tell I&#8217;ve been coding my week-end away, can you). Normal people, please skip over.<\/p>\n<p><!--more-->When working with MySQL and PHP, clean time conversion can become a real pain in the arse.<\/p>\n<p>You ought to always store your timestamps as GMT in your DB, no matter what.<br \/>\nProblem is, when you feed the text value returned by MySQL into PHP <a href=\"http:\/\/php.net\/strtotime\"><code> strtotime()<\/code><\/a>, it is not read as a GMT time! Check it by converting it back to a text format (using <a href=\"http:\/\/php.net\/strftime\"><code> strftime()<\/code><\/a>): you&#8217;ll see it doesn&#8217;t match the original GMT time such as returned by MySQL.<\/p>\n<p>To make all this work, you simply need to ensure it is read as GMT by PHP by appending the timezone format to the string returned by MySQL. <\/p>\n<p>An example:<\/p>\n<blockquote><p><code><br \/>\necho $my_sql_gmt_time; \/\/ 2005-03-13 15:00:12<\/p>\n<p>echo $bad_ts = strtotime($my_sql_gmt_time); \/\/ 1110747612 < - bad\necho strftime($bad_ts); \/\/ 15:00:12 CST <- bad\n\necho $good_ts  = strtotime($my_sql_gmt_time . \" GMT\"); \/\/ 1110726012 <- good\necho strftime($good_ts); \/\/ 09:00:12 CST <-> 15:00:12 GMT < - good\n<\/code><\/code><\/p><\/blockquote>\n<p>This very stupid little glitch (why would PHP assume CST timezone when most functions use GMT?) took me twenty minutes to debug, despite my semi-thorough reading of the PHP doc for the relevant functions, so I figured I&#8217;d spare the time to the next fellow google searcher.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another post of utter geekery with some highly uninteresting and very unnecessary programming tidbit (I bet you can&#8217;t tell I&#8217;ve been coding my week-end away, can you). Normal people, please skip over.<\/p>\n<p>GMT conversion between mySQL and PHP<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[36],"tags":[],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/posts\/724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":0,"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknowngenius.com\/blog\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}