{"id":939,"date":"2011-05-22T02:06:41","date_gmt":"2011-05-22T09:06:41","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=939"},"modified":"2011-05-22T02:06:41","modified_gmt":"2011-05-22T09:06:41","slug":"php-function-to-convert-time-from-one-timezone-to-another","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2011\/05\/22\/php-function-to-convert-time-from-one-timezone-to-another\/","title":{"rendered":"PHP function to convert time from one timezone to another"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPhp.js\"><\/script>\n<p>Some applications require finding out what the current time would be in a particular timezone. Another similar requirement would be to convert a given date\/time value to a different timezone.<\/p>\n<p>PHP has very powerful date handling functions and it makes the above very simple and easy. We have tried to use quite a few readymade timezone conversion classes but either they had a bug or they were too complicated. Hence we came up with our own class. The actual conversion code is only about 4 lines of code.<\/p>\n<p>What it does is convert the time string argument from the source timezone into GMT time and then convert into the target timezone. This approach was far simpler than trying to directly convert from source timezone into the target timezone.<\/p>\n<p><pre class=\"brush: php\">class convert_timezone\r\n{\r\n\tfunction convert_timezone() \/\/ Constructor of the class\r\n\t{\r\n\t}\r\n\r\n\tfunction conver_to_time($conv_fr_zon=0,$conv_fr_time=&quot;&quot;,$conv_to_zon=0)\r\n\t{\r\n\t\t\/\/echo $conv_fr_zon.&quot;&lt;br&gt;&quot;;\r\n\t\t$cd = strtotime($conv_fr_time); \r\n\t\t\r\n\t\t$gmdate = date(&#039;Y-m-d H:i:s&#039;, mktime(date(&#039;H&#039;,$cd)-$conv_fr_zon,date(&#039;i&#039;,$cd),date(&#039;s&#039;,$cd),date(&#039;m&#039;,$cd),date(&#039;d&#039;,$cd),date(&#039;Y&#039;,$cd))); \r\n\t\t\/\/echo $gmdate.&quot;&lt;br&gt;&quot;;\r\n\t\t\t\t\r\n\t\t$gm_timestamp = strtotime($gmdate);\r\n\t\t$finaldate = date(&#039;Y-m-d H:i:s&#039;, mktime(date(&#039;H&#039;,$gm_timestamp )+$conv_to_zon,date(&#039;i&#039;,$gm_timestamp ),date(&#039;s&#039;,$gm_timestamp ),date(&#039;m&#039;,$gm_timestamp ),date(&#039;d&#039;,$gm_timestamp ),date(&#039;Y&#039;,$gm_timestamp ))); \r\n\t\t\r\n\t\treturn $finaldate;\r\n\t}\r\n}\r\n<\/pre><\/p>\n<p>Example usage would be:<\/p>\n<p><strong>$c = new convert_timezone();<\/p>\n<p>$resultTime = $c->conver_to_time(5.30, &#8220;2011-05-09 11:00:00&#8221;, -13);<\/p>\n<p>$resultTime = $c->conver_to_time(-4, &#8220;2011-05-09 11:00:00&#8221;, 10);<\/strong><\/p>\n<p>The first one converts 5th May, 2011, 11 am from the timezone GMT+0530hrs to GMT-1300hrs<\/p>\n<p>The second one coverts 5th May,2011 11 am from the timezone GMT-0400hrs to GMT1000 hrs<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Some applications require finding out what the current time would be in a particular timezone. Another similar requirement would be to convert a given date\/time <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2011\/05\/22\/php-function-to-convert-time-from-one-timezone-to-another\/\" title=\"PHP function to convert time from one timezone to another\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10],"tags":[80,125,130,200,249,251],"class_list":["post-939","post","type-post","status-publish","format-standard","hentry","category-apachephp","category-programming","tag-convert","tag-format","tag-function","tag-php","tag-time","tag-timezone"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=939"}],"version-history":[{"count":0,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/939\/revisions"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}