{"id":1365,"date":"2017-04-07T16:13:51","date_gmt":"2017-04-07T20:13:51","guid":{"rendered":"https:\/\/easy-admin.ca\/?p=1365"},"modified":"2017-08-21T10:23:19","modified_gmt":"2017-08-21T14:23:19","slug":"how-to-change-opencart-timezone","status":"publish","type":"post","link":"https:\/\/easy-admin.ca\/index.php\/2017\/04\/07\/how-to-change-opencart-timezone\/","title":{"rendered":"How to change OpenCart Timezone"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1366 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone.jpg\" alt=\"\" width=\"565\" height=\"275\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone.jpg 565w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-300x146.jpg 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<p>OpenCart used globally and operated from different timezone based on site owner place. In most case, OpenCart site run in UTC timezone. This affecting all aspect depend on the time configuration, from product comment, special price to order history.<\/p>\n<p>This is not issue if you can accept the time difference; but of course it\u2019s not logical for the site owner confused either an order is placed today or yesterday because the OpenCart site time is different from their local.<\/p>\n<p>This tutorial covering not just change PHP timezone but also synchronize it with database. Lets start the tutorial with most case assumption that the OpenCart site is run on UTC timezone. To visualize this we use the free extension System Information as on image below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1367 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-02.jpg\" alt=\"\" width=\"565\" height=\"338\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-02.jpg 565w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-02-300x179.jpg 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<p>For this tutorial, I use OpenCart v2.2.x that might be different from your OpenCart version. In order to use this tutorial for your site, you need to do adjustment where the code should change.<\/p>\n<h4>Change PHP Timezone<\/h4>\n<p>There is two method to change PHP timezone, through php.ini or set them at PHP file directly. To change timezone in php.ini you can ask your hosting provider, because one host to another have different approach.<\/p>\n<p>To use the second method, we will change <strong>system\/startup.php<\/strong> with PHP supported timezone.<\/p>\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"plain plain\">\/\/ if (!ini_get('date.timezone')) {\u00a0\u00a0\u00a0\u00a0\u00a0 <\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"plain plain\">\/\/\u00a0\u00a0 date_default_timezone_set('UTC');\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"plain plain\">\/\/ }<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"plain plain\">date_default_timezone_set('Australia\/Sydney');<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><\/div>\n<\/div>\n<p>On code above I commented the OpenCart code because I have <em>date.timezone<\/em> configured in php.ini. This change will give us result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1368 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-03.jpg\" alt=\"\" width=\"565\" height=\"338\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-03.jpg 565w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-03-300x179.jpg 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<p>Most tutorial on change OpenCart timezone finish at this stage. But this is actually wrong. I purposely set the timezone to Australia\/Sydney because I know the time different when write this tutorial will be one day with database time that still use UTC.<\/p>\n<p>If you add product review now, which is added to database with sql query <em>now()<\/em> the date added is 20\/03\/2016, even though PHP time is March 21, 2016. This will make you think the review is maded yesterday, even though it\u2019s just 5 minute ago.<\/p>\n<p>OpenCart 2.2 have 275+ sql query that use <em>now()<\/em> and this mean the same thing will happen to Product Special Price, Coupon Code, Voucher, Order, Return and more. If you change PHP timezone, make sure to synchronize with database.<\/p>\n<h4>Synchronize PHP and Database Timezone<\/h4>\n<p>Synchronize php and database timezone is done by set the database connection timezone. We can try to set global database timezone but that need super privillage user which is not available on shared hosting. Query we use to set database timezone:<\/p>\n<p><code class=\"plain plain\">SET time_zone='offset';<\/code><\/p>\n<p>We use offset and not named timezone because, quoted from MySQL: <em>\u201cNamed time zones can be used only if the time zone information tables in the mysql database have been created and populated\u201d<\/em>. There is no guarantee all hosting have timezone information table in their database instances.<\/p>\n<p>To change OpenCart database connection timezone, we will change <strong>system\/library\/db\/mysqli.php<\/strong> and add code below after <em>$this-&gt;connection-&gt;query(\u201cSET SQL_MODE = \u201d\u201d);<\/em><\/p>\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"plain plain\">$dt = new \\DateTime();<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"plain plain\">$this-&gt;connection-&gt;query(\"SET time_zone='\" . $dt-&gt;format('P') . \"';\");<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><\/div>\n<div class=\"line number2 index1 alt1\">\n<p>In OpenCart 2.0.x and 2.1.x change <strong>$this-&gt;connection<\/strong> to <strong>$this-&gt;link<\/strong>.Since System Information use OcMod to get Database info, it\u2019s important to refresh modification at <strong>Extensions &gt; Modification<\/strong> before check the result. This change will give us result:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1369 size-full\" src=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-04.jpg\" alt=\"\" width=\"565\" height=\"338\" srcset=\"https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-04.jpg 565w, https:\/\/easy-admin.ca\/wp-content\/uploads\/2017\/04\/how-to-change-opencart-timezone-04-300x179.jpg 300w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/p>\n<p>You can see on image above that date time on PHP and Database is now synchronized.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>OpenCart used globally and operated from different timezone based on site owner place. In most case, OpenCart site run in UTC timezone. This affecting all aspect depend on the time configuration, from product comment, special price to order history. This is not issue if you can accept the time difference; but of course it\u2019s not &hellip; <a href=\"https:\/\/easy-admin.ca\/index.php\/2017\/04\/07\/how-to-change-opencart-timezone\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to change OpenCart Timezone<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"How to change OpenCart Timezone - HP Server","description":"OpenCart used globally and operated from different timezone based on site owner place. In most case, OpenCart site run in UTC timezone. This affecting all aspec"},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1365","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/1365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/comments?post=1365"}],"version-history":[{"count":0,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"wp:attachment":[{"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/easy-admin.ca\/index.php\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}