Post by butte » Sat Nov 09, 2013 11:00 am

Well, 5.3 likes region/city format and calls sensible gmt deprecated. In ancient times we not only got to do our own arithmetic, we had to do it. Now we have to know geography instead -- not degrees at 15 of those per hour, but placenames unrelated to the reasons for degrees and all of that. Progress; not.

Guru Member

Posts

Joined
Wed Mar 20, 2013 6:58 am

Post by robzcuy » Sat Nov 09, 2013 4:35 pm

So, in other words, its impossible to adjust my website time and date exactly the same as my date and time on my country ?

-__-

Newbie

Posts

Joined
Sat Nov 24, 2012 1:03 pm

Post by butte » Sat Nov 09, 2013 11:04 pm

Short answers are, no, and if.

The practical answer on timezones in php is not always short.

Confusion can arise where you are in one location, your host is in another (based primarily in, say, London or Redmond or Houston or Santa Rosa), and your machine is in yet another (respectively Illinois or California or Utah or Texas). That seems to have fostered some of the confusion well above where offsets were not making complete sense to people relative to host, zero longitude (gmt, utc), and hometown.
____________________________________________________
(1) Your host might allow you access to a reasonably full php.ini for your account, perhaps in basic and advanced views. (The server's own master php.ini is on the order of 2,000 lines long, you will not be allowed to touch those.) In such a php.ini are these "timezone" entries (for the nn.mmm formats see further down):

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
; Defines the default timezone used by the date functions
;date.timezone =
;date.default_latitude = nn.mmmm
;date.default_longitude = nn.mmmm
;date.sunrise_zenith = nn.mmmm
;date.sunset_zenith = nn.mmmm

date.timezone = "UTC"

You can likewise also uncomment (remove ;) these two so as to have:
date.default_latitude = [use the nn.mmmm format explained below] ; precede with a ";" any notes to yourself
date.default_longitude = [use the nn.mmmm format explained below] ; precede with a ";" any notes to yourself

You can use phpinfo.php to see what you've accomplished that you may need to edit.
____________________________________________________
(2) One group of specifics, with examples:

http://www.php.net/manual/en/function.date-date-set.php
date_date_set — Alias of DateTime::setDate

http://www.php.net/manual/en/function.d ... ne-get.php
date_default_timezone_get — Gets the default timezone used by all date/time functions in a script

http://www.php.net/manual/en/function.d ... ne-set.php
date_default_timezone_set — Sets the default timezone used by all date/time functions in a script
____________________________________________________
(3) Another group of specifics:

Supported Date and Time Formats
Time Formats
Date Formats
Compound Formats
Relative Formats
in detail at:
http://www.php.net/manual/en/datetime.formats.time.php
http://www.php.net/manual/en/datetime.formats.date.php
http://www.php.net/manual/en/datetime.f ... mpound.php
http://www.php.net/manual/en/datetime.f ... lative.php
____________________________________________________
(4) More than you ever wanted to know about time a la php is outlined here:

http://www.php.net/manual/en/book.datetime.php
at . . .

date_time_set — Alias of DateTime::setTime
date_timestamp_get — Alias of DateTime::getTimestamp
date_timestamp_set — Alias of DateTime::setTimestamp
date_timezone_get — Alias of DateTime::getTimezone
date_timezone_set — Alias of DateTime::setTimezone
date — Format a local time/date
getdate — Get date/time information
gettimeofday — Get current time
gmdate — Format a GMT/UTC date/time
gmmktime — Get Unix timestamp for a GMT date
gmstrftime — Format a GMT/UTC time/date according to locale settings
idate — Format a local time/date as integer
localtime — Get the local time
microtime — Return current Unix timestamp with microseconds
mktime — Get Unix timestamp for a date
strftime — Format a local time/date according to locale settings
strptime — Parse a time/date generated with strftime
strtotime — Parse about any English textual datetime description into a Unix timestamp
time — Return current Unix timestamp
timezone_abbreviations_list — Alias of DateTimeZone::listAbbreviations
timezone_identifiers_list — Alias of DateTimeZone::listIdentifiers
timezone_location_get — Alias of DateTimeZone::getLocation
timezone_name_from_abbr — Returns the timezone name from abbreviation
timezone_name_get — Alias of DateTimeZone::getName
timezone_offset_get — Alias of DateTimeZone::getOffset
timezone_open — Alias of DateTimeZone::__construct
timezone_transitions_get — Alias of DateTimeZone::getTransitions
timezone_version_get — Gets the version of the timezonedb

Guru Member

Posts

Joined
Wed Mar 20, 2013 6:58 am

Post by beanbagshop » Fri Jan 31, 2014 8:37 pm

Yes Sir, This can be very confusing for a non developer user, My advice is to go with a module.
Opencart Extension store has a few which can solve this issue of Local Time for the store.

Regards

Image
Bean Bags Online Shopping | Bean Bags | Bean Bags Refill
Bean Bag Shop Online India | Bean Bag Chairs Online


New member

Posts

Joined
Sun Apr 28, 2013 2:45 pm
Location - Pune, Maharastra

Post by butte » Fri Jan 31, 2014 11:29 pm

Free and fee both. Whether you ask local time or time zone or all three, extensions for that do not all appear on the first page or two. Pay attention to version compatibilities, and test, that is not a good thing to have go haywire.

Guru Member

Posts

Joined
Wed Mar 20, 2013 6:58 am

Post by sujeet » Tue Feb 11, 2014 7:38 pm

You can use this extension to solve your purpose. Its very easy to install and use. Try this:
http://www.opencart.com/index.php?route ... n_id=15772

Newbie

Posts

Joined
Tue Feb 04, 2014 2:33 pm

Post by PlusCybernet » Sat May 10, 2014 1:26 am

After following instructions in this thread ... it's showing + 30 minutes ... how I can adjust that?

User avatar
Active Member

Posts

Joined
Sun Dec 25, 2011 12:01 pm

Post by paisleybay » Wed Dec 24, 2014 10:39 am

@PlusCybernet

I saw your post dated May 9, 2014 reading as under:
After following instructions in this thread ... it's showing + 30 minutes ... how I can adjust that?
I am having exact same problem. Did you get around this? If so kindly share. Thanks & regards.

New member

Posts

Joined
Sat Dec 08, 2012 1:55 am

Post by iamafaker » Mon Mar 02, 2015 2:04 am

tried modifying both startup.php and php.ini, NOT working :'( :'( :'(

Active Member

Posts

Joined
Wed Sep 18, 2013 3:19 pm

Post by hohosiu » Wed Apr 01, 2015 3:11 pm

Hi,

I am using OC 2.0.1.1 version, I have done the code changes above, and orders Added is with correct time but Orders Modified still having wrong time. Does anyone know why is that?

Much appreciated.

Newbie

Posts

Joined
Mon Nov 24, 2014 11:26 pm

Post by hohosiu » Thu Apr 02, 2015 11:19 am

anyone has any idea?

Newbie

Posts

Joined
Mon Nov 24, 2014 11:26 pm

Post by hohosiu » Thu Apr 02, 2015 12:11 pm

Looks like the code in system/database/mysql.php is not executed even I have new orders inserted, wonder what I have missed?

Newbie

Posts

Joined
Mon Nov 24, 2014 11:26 pm

Post by streetpreacher » Fri Feb 05, 2016 3:08 pm

Thanks for the posted information, keithb. You have helped me solve my problem. However, all I needed to do was change this line, mysql_query("SET TIME_ZONE = '0'");, to include my offset hour from GMT 0:00. I am 5 hours behind GMT so my offset hour is -5. If you know you offset hour, simply go to /system/database/mysql.php and edit mysql_query("SET TIME_ZONE = '-5:00'");. You will find it after the line mysql_query("SET SQL_MODE = ''", $this->link);

Before:

Code: Select all

mysql_query("SET SQL_MODE = ''", $this->link);
mysql_query("SET TIME_ZONE = '0:00'");
After:

Code: Select all

mysql_query("SET SQL_MODE = ''", $this->link);
mysql_query("SET TIME_ZONE = '-5:00'");
That's it. O0


Posts

Joined
Fri Feb 05, 2016 2:13 pm

Post by sunsys » Sat Feb 06, 2016 4:59 am

streetpreacher wrote:Thanks for the posted information, keithb. You have helped me solve my problem. However, all I needed to do was change this line, mysql_query("SET TIME_ZONE = '0'");, to include my offset hour from GMT 0:00. I am 5 hours behind GMT so my offset hour is -5. If you know you offset hour, simply go to /system/database/mysql.php and edit mysql_query("SET TIME_ZONE = '-5:00'");. You will find it after the line mysql_query("SET SQL_MODE = ''", $this->link);

Before:

Code: Select all

mysql_query("SET SQL_MODE = ''", $this->link);
mysql_query("SET TIME_ZONE = '0:00'");
After:

Code: Select all

mysql_query("SET SQL_MODE = ''", $this->link);
mysql_query("SET TIME_ZONE = '-5:00'");
That's it. O0
The above is not present in oc ver2.0.3.1 so how to change the time zone in that case, I have tried php.ini, index.php, .htaccess though the time zone has changed on the server(checked using phpinfo.php) but the orders placed do not reflect the actual or real time of my zone(GMT +5.30). Any help would be greatly appreciated.

Thank you for your time

Regards,
Sun Systems
Industrial Electronics and Instrumentation


User avatar
Active Member

Posts

Joined
Tue Jan 27, 2015 5:19 am

Post by vincent_angbt » Mon Feb 22, 2016 10:53 am

keithb wrote:Sometimes in OpenCart the effect of different timezones can affect what is stored in the database and displayed to the user.

Whilst the defaults work correctly on my local pc I found that on a Live server they did not and Dates/Times for Orders and Order History were not being displayed as I would like

The issue is that typically the Web server running PHP can be set to a different timezone to the database server and one cannot always get sufficient access to change the settings permanently, especially if on shared servers.

I have seen several posts here about partial solutions for this and the best I could find was the free module storetime by Fido-X http://forum.opencart.com/viewtopic.php?f=119&t=30188 but even this does not currently address the issue when the database needs changing and not just the web server/php. So even this did not fully sort my problem.

I live in the UK but my hosting is in the America/Chicago TimeZone and I wanted my reports / cut-off times etc to be in my local UK time

So at say 15:33 UK time (allowing +1 hour as we are on BST (British Summer Time) at the moment)
I would expect a UTC/GMT timezone to show/display as 14:33
However Chicago is -5 hours from UCT it showed as 9:33am
i.e. a total of -6 hours different which was very confusing/misleading with Order Histories

Now there are various approaches to using timezones and whether one should store everthing in UTC and then convert just for display purposes in the local timezone or whether to store all values as from the local timezone.

If the dates were stored in datestamp columns (which are simply Integers with on offset in seconds from a Unix start date) then any local dates would get put into the database as UTC and converted back automatically to local dates as they were retrieved. This makes for easy datetime arithmetic but has the overhead of changing these values.
However I did not want to change the database schema from the currently used datetime fields for this in case there were any side-effects elsewhere in the code (datetime columns just store what they are passed and often the servers use UTC as a standard).

As I found nearly 200 instances of the mysql function NOW() used in the modules I decided I wanted to store the datetime fields as actual local UK time but I didn't want to alter every occurrence of NOW() in the code.

I also wanted to allow for any British Summer Time changes.
I couldn't set the database using TimeZone names as not all servers have this configured correctly so I needed to use the offset for it to always work.

So overall I decided to take the approach of
1. Setting the webserver/php timezone to the one I wanted (Europe/London in my case)
2. Calculate what Offset this TimeZone had from UTC/GMT
3. Use this Offset to set the Database to this TimeZone Offset

This is the way I did it though the code may well get improved by others!

There are timezone settings within PHP which are currently set in system/startup.php around line 50
This only sets a value using date_default_timezone_set if there is NO current setting so I changed it to always set my required TimeZone

Remove

Code: Select all

if (!ini_get('date.timezone')) {
     date_default_timezone_set('UTC');
}
Replace with (or the required timezone)

Code: Select all

date_default_timezone_set('Europe/London');
I also have QPhoria's excellent VQMod and I found there was a similar line there
In vqmod\vqmod.php
Find and delete

Code: Select all

date_default_timezone_set('UTC');
The main code logic went into system/database/mysql.php so is only called once per page.
(I fdidn't use any configs as they are set much later in the process after the initial database connection)

After

Code: Select all

mysql_query("SET SQL_MODE = ''", $this->connection);
Add

Code: Select all

// get local time on Web/PHP server
$localtime = strtotime(date('Y-m-d H:i:s'));

//get local time in GMT/UTC (i.e GMT/UTC is set as +0:00 on database and other timezones are set as +/- hours of this)
$gm_localtime = strtotime(gmdate('Y-m-d H:i:s'));

//find offset in hours (if any - which allows for Daylight Saving Time or British Summer Time (BST))
$diff_hours = ($localtime - $gm_localtime) / 3600;

//Then the Database server needs to be set to this Offset to store/retrieve values as local ones
$adjust = "SET time_zone = '";

if ($diff_hours > 0) {
	$adjust .= "+" . ceil($diff_hours);
} elseif ($diff_hours < 0) {
	$adjust .= floor($diff_hours);
} else {
	$adjust .= "+0";
}	
$adjust .= ":00'";

mysql_query($adjust, $this->connection);
This solved my problem but it depends on your hosting as to whether you need or want to use it or not. :-\
Hi, how can this thing work in version 2.1 ?

New member

Posts

Joined
Tue Dec 17, 2013 11:35 am

Post by 2valenki » Sat Apr 30, 2016 12:29 am

Voila! Thank you, keithb. I searched for the solution of mismatch of time showing up in error.log and I've found it! ;D
Opencart version: 2.2

Newbie

Posts

Joined
Wed Apr 27, 2016 4:38 am

Post by burakcarkci » Wed Oct 25, 2017 3:10 pm

There is a free module on opencart market place to do this and it's super easy!

https://www.opencart.com/index.php?rout ... r_rating=2

Newbie

Posts

Joined
Thu Sep 28, 2017 11:41 am
Who is online

Users browsing this forum: No registered users and 70 guests