Tutorial 2 - How to Change Time Zone in OpenCart

Tutorial 2 - How to Change Time Zone in OpenCart

Technically, How to Change Time Zone in OpenCart is quite simple. It's just that before going there we must first know the reasons why we need to change the timezone in OpenCart. Before going any further into our tutorial how to change timezone in OpenCart. There is an OpenCart extension that is very useful for users, namely System Information. This extension is specifically made to detect the OpenCart working environment. We can see a lot of system information including lag time in PHP and database.

Why do we have to set the timezone in OpenCart?

OpenCart itself is made in the PHP language where the timezone setting will be the reference used by all date and time functions in the script. This is essential in order for any process that takes time to run well. Relevant to the OpenCart system, with the correct timezone setting, time-related matters such as discounts, special products, etc. can function as desired.

In some cases it can happen that the time which is stored in the database is not the same as that which is displayed in the user. For example, order history is very sensitive to timing.

Is the timezone setting mandatory for OpenCart?

In general, Yes !. By default OpenCart will make UTC timezone as default timezone.


Then because by default it is made for online trading globally. Some owners who store their website files on foreign servers, this is mandatory. For example, we are in Jakarta, Indonesia which has the Asia / Jakarta time zone. 7 hours difference from GMT (GMT +7). If the hosting is in Chicago, America. Then the difference is 12 hours. So if all data entry is carried out in Jakarta, it must be adjusted according to the time in Jakarta. Not a time server in Chicago. This means that it must be offset by 12 hours.

Change Time Zone in OpenCart

In general settings must be made for PHP and MySQL systems. Why is that?. Because the database server may not be in the same time zone as the PHP server. In fact, there are many SQL statements in OpenCart that use the NOW () function, which means taking the TIMESTAMP on the MySQL server at that time.

There are a number of things that must be done to change the time zone which are described below.

PHP Time Zone Settings

Access system / startup.php and find the following line of code:

if (!ini_get ('date.timezone')) {
date_default_timezone_set ('UTC');
}

Comment the code above and add a new line of code:

date_default_timezone_set('Asia/Jakarta');

Set to Asia / Jakarta for the time zone WIB (West Indonesia Time).

Setting Time Zone Database

Access system/library/db/mysqli.php. Search for a code phrase:

$this->connection->query("SET SQL_MODE = ''");

or, code

$this->link->query("SET SQL_MODE = ''");

in OpenCart 2.0.1.1
then add the following code after it:

$dt = new \DateTime();

$this->connection->query("SET time_zone='" . $dt->format('P'). 
"';");

The code above will set the time zone with the resulting offset from $ dt-> format ('P').

Now look at System Information.

Time Zone Setting Test

Apart from that, you can also test orders in real time and view the logged times in the database. If the time is the same as the time you ordered, then the time zone setting is successful.

Until here we have set the timezone correctly. That's how to change the timezone in OpenCart.