Welcome Guest, Not a member yet? Register   Sign In
local to gmt function doesn't work
#1

(This post was last modified: 07-11-2017, 07:27 PM by ardavan.)

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();
echo 
'Local : '.nice_date(unix_to_human($local),'Y-m-d h:i A');
echo 
'GMT : '.nice_date(unix_to_human(local_to_gmt($local)),'Y-m-d h:i A'); 

And the result will be
Code:
Local : 2017-07-11 09:33 PM
GMT : 2017-07-11 07:33 PM

But the GMT time is not correct. At this time the GMT time is 1:33 PM NOT 7:33 PM

  1. by changing the $config['time_reference'] the application timezone will change?
  2. How to check which Timezone is being used by CodeIgniter?
  3. Is this a correct way to make use of Date helper to achieve my goal?
  4. How can I fix this?

Thanks in advance
Reply
#2

GMT is correct, but I think your $config['time_reference'] is incorrect.

The explanation in the config.php is
PHP Code:
/*
|--------------------------------------------------------------------------
| Master Time Reference
|--------------------------------------------------------------------------
|
| Options are 'local' or any PHP supported timezone. This preference tells
| the system whether to use your server's local time as the master 'now'
| reference, or convert it to the configured one timezone. See the 'date
| helper' page of the user guide for information regarding date handling.
|
*/ 

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
Reply
#3

I have tried both with SGT and Asia/Singapore and the result still is the same!
Reply
#4

(This post was last modified: 07-11-2017, 07:02 PM by skunkbad.)

More than one way to skin a cat:

PHP Code:
<?php
$date 
= new DateTime('now', new DateTimeZone('Europe/Berlin'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('Asia/Singapore'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('UTC'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('America/Los_Angeles'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />"
DateTime class > CI date helper
Reply
#5

(07-11-2017, 06:47 PM)skunkbad Wrote: More than one way to skin a cat:

PHP Code:
<?php
$date 
= new DateTime('now', new DateTimeZone('Europe/Berlin'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('Asia/Singapore'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('UTC'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('America/Los_Angeles'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />"
DateTime class > CI date helper

Why can't use the Time_Referenece only to change the timezone?
Reply
#6

(07-11-2017, 07:21 PM)ardavan Wrote:
(07-11-2017, 06:47 PM)skunkbad Wrote: More than one way to skin a cat:

PHP Code:
<?php
$date 
= new DateTime('now', new DateTimeZone('Europe/Berlin'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('Asia/Singapore'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('UTC'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />";

$date->setTimezone(new DateTimeZone('America/Los_Angeles'));

$tz $date->getTimezone();

echo 
$date->format('Y-m-d H:i:s') . " in " $tz->getName() . "<br />"
DateTime class > CI date helper

Why can't use the Time_Referenece only to change the timezone?

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 ...
Reply
#7

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($timezoneconfig_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'))
{
    
/**
     * Get "now" time
     *
     * Returns time() based on the timezone parameter or on the
     * "time_reference" setting
     *
     * @param    string
     * @return    int
     */
    
function now($timezone NULL)
    {
        if (empty(
$timezone))
        {
            
$timezone config_item('time_reference');
        }
        
        
var_dump(config_item('time_reference'));

        if (
$timezone === 'local' OR $timezone === date_default_timezone_get())
        {
            return 
time();
        }

        
$datetime = new DateTime('now', new DateTimeZone($timezone));
        
sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d'$day$month$year$hour$minute$second);

        return 
mktime($hour$minute$second$month$day$year);
    }

Reply
#8

(07-12-2017, 01:07 AM)Martin7483 Wrote: I have just been testing.

I did a var_dump() on $timezone and config_item('time_reference') in the date_helper function now()

That's funny because when I run your code
PHP Code:
var_dump($timezoneconfig_item('time_reference')); 
i got
Code:
NULL string(14) "Asia/Singapore"
Where did you get $timezone variable?
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?
Reply
#9

(This post was last modified: 07-13-2017, 02:32 AM by Martin7483.)

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
$date "2015-04-13 09:22:23";
// If you want to know what the GMT was at that time, append the timezone and convert to a UNIX timestamp
$local_time strtotime($date ." ".config_item('time_reference'));
// Then use local_to_gmt to get the GMT
$gmt_date date('Y-m-d H:i:s'local_to_gmt($local_time)); 

Hope my explanation is clear
Reply




Theme © iAndrew 2016 - Forum software by © MyBB