Mohammad Naji
Mohammad Naji

Reputation: 5442

Convert user local time to gmt unix timestamp in codeigniter

If you load codeigniter's date helper, there will be the function "local_to_gmt()" but it takes no argument and only converts current server time (that is generated by time() ).

I need the function to be able to convert any other time given from user according to his local time and DST fields in database.

Or a function like human_to_unix() with 2 more parameters: dst and local time.

Does such a function exist?


I had said: local_to_gmt() takes no argument, but as the first answerer says it DOES take an argument as well. But I'm focusing on user timezone and daylight saving options.


THE 3RD UPDATE!!!

I have found a solution but according to stackoverlow's laws I couldn't answer my own question until some hours later.

Look at this function that might be the nearest solution for me until now: http://codeigniter.com/wiki/convert_to_gmt

function convert_to_gmt($time = '', $timezone = 'UTC', $dst = FALSE)
{            
    if ($time == '')
    {
        return now();
    }

    $time -= timezones($timezone) * 3600;

    if ($dst == TRUE)
    {
       $time -= 3600;
    }

    return $time;
} 

But there is still a question:

Do you believe that the $dst will work without any problem? As I understand, according to the function the $dst will ALWAYS decrease one hour. But it should be realized weather it is summer or not. It will decrease the time even if it's winter!!!

How to overcome this problem???

Upvotes: 2

Views: 3316

Answers (2)

Mohammad Naji
Mohammad Naji

Reputation: 5442

Using gm group functions in PHP like:

When I asked the question, I didn't know that PHP has those functions, so I'd looked up such a behavior in Codeigniter.

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146480

Manual says:

local_to_gmt()
Takes a Unix timestamp as input and returns it as GMT

... and provides this example:

$now = time();
$gmt = local_to_gmt($now);

So I'd dare say that it actually accepts an argument.

Upvotes: 1

Related Questions