local to gmt function doesn't work |
My server(localhost) timezone is Europe/Berlin.
I set both $config['time_reference'] = 'SGT'; and $config['time_reference'] = 'Asia/Singapore'; in config.php which is singapore time. now I need to convert the local DateTime to GMT by using this code PHP Code: $local = now(); And the result will be Code: Local : 2017-07-11 09:33 PM But the GMT time is not correct. At this time the GMT time is 1:33 PM NOT 7:33 PM
Thanks in advance
GMT is correct, but I think your $config['time_reference'] is incorrect.
The explanation in the config.php is PHP Code: /* Note the first line: Options are 'local' or any PHP supported timezone If you look at PHP.net an expected timezone for Singapore = Asia/Singapore So if the set timezone in not supported, it will fail and use the system default. Which in your case is Europe/Berlin which explains the 2 hour difference between your Local and GMT time
I have tried both with SGT and Asia/Singapore and the result still is the same!
More than one way to skin a cat:
PHP Code: <?php (07-11-2017, 06:47 PM)skunkbad Wrote: More than one way to skin a cat: Why can't use the Time_Referenece only to change the timezone? (07-11-2017, 07:21 PM)ardavan Wrote:(07-11-2017, 06:47 PM)skunkbad Wrote: More than one way to skin a cat: I don't know, but I never use it. I always use PHP native classes and functions when they do what I need. Maybe somebody else will give you an answer, but my code works ...
I have just been testing.
I did a var_dump() on $timezone and config_item('time_reference') in the date_helper function now() PHP Code: var_dump($timezone, config_item('time_reference')); They both returned the correct value that I set in $config["time_reference"]; And the output gave me the correct time for Singapore I think that somewhere in your code you are overwriting the $config["time_reference"] Your server environment is not the problem as the solution that skunkbad gave is working. But that solution is the exact same code that is used in the now() function of the date_helper PHP Code: if ( ! function_exists('now')) (07-12-2017, 01:07 AM)Martin7483 Wrote: I have just been testing. That's funny because when I run your code PHP Code: var_dump($timezone, config_item('time_reference')); Code: NULL string(14) "Asia/Singapore" I'm not touching any timezone in other places in my entire project. just started to work on it and only I changed the time_reference. The now() is working perfectly as you can see you will get the correct timing of the region BUT the problem is the local_to_gmt() function that returns based on my server timezone (Europe/Berlin). Do you know what's going on in the local_to_gmt() function?
First, I think you misread my post. I did the var_dump WITHIN the function NOW()
and not from where I was calling the function Second: After some searching I found this on Stack Overflow Quote:To convert a string representation of a time into a time_t value, use strtotime. If the string contains a time zone reference, strtotime will do the conversion for you. If not, you can use date_default_timezone_set to tell PHP what timezone to use when interpreting the string. So when converting a local time to GMT time, the date time string that is converted to a timestamp should have the timezone included when using strtotime(). If the timezone is not present when using strtotime, strtotime will use the default timezone of the server. When you use NOW(), you are asking for the current date and time for the set timezone. Converting that value to GMT makes no sence, because it will return the current GMT To get the current GMT time, just call local_to_gmt() without passing a value to the function To convert a stored date time to GMT see this example PHP Code: // If your local date is Hope my explanation is clear |
Welcome Guest, Not a member yet? Register Sign In |