2

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???

Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) - Also please see the many related questions on the right and use the site search. – hakre Jan 19 '12 at 11:55
  • There is no relation between my question and the link you commented. I'm talking about user specific timezone and DST setting. But none of the question and the best answer (I only read those 2) were about user local time; thnx anyway – Mohammad Naji Jan 19 '12 at 12:02
  • It's not clever to ask for a function that does what you need but better to look how it's done. The duplicate was not really fitting I must confess, but I also pointed you to other related questions on the right, e.g. (just picking *one* of the many) [PHP, Codeigniter: How to Set Date/Time based on users timezone/location globally in a web app?](http://stackoverflow.com/questions/2274568/php-codeigniter-how-to-set-date-time-based-on-users-timezone-location-globally) – hakre Jan 19 '12 at 12:06

2 Answers2

1

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Oh, yes, you are right. But it doesn't care about user local time and DST settings. – Mohammad Naji Jan 19 '12 at 12:04
  • Unix timestamps are "universal", they don't contain time zone information because they are the same no matter where you are. You take time zone into account when you convert from/to regular dates (e.g. 19/01/2012 13:14). – Álvaro González Jan 19 '12 at 12:14
  • Yes, you are right. And because of that I want a function to affect the time user gives (according to his database fields in user table) and then convert it to unix timestamp. This way, I will be able to have my own calculations on it, and I'll also be able to show the time in other users' local times. – Mohammad Naji Jan 19 '12 at 12:19
  • To be honest, I'm not a CI user. I had expected that is was a simple case of manual misread. Sorry I couldn't be more helpful. – Álvaro González Jan 19 '12 at 12:32
  • Dear friend, I really appreciate your tries. Thank you very much for your comments :) – Mohammad Naji Jan 19 '12 at 12:34
0

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.

Mohammad Naji
  • 5,372
  • 10
  • 54
  • 79