Welcome Guest, Not a member yet? Register   Sign In
now() function does not return GMT time; even when config is set to.
#1

[eluser]Drew J[/eluser]
This has been driving me crazy! I'm starting to second guess myself and results after testing this every which-a-way.

I have my $config['time_reference'] = 'gmt'; set correctly in my config file. I've checked this like 5 times now.

Javascript is giving me: 1286775271 -- this is had by Math.floor((new Date().getTime() / 1000));
now() is giving me: 1286800471
gmdate() is giving me: 1286775271

I put a stop in the now() function to ensure it's getting the GMT time, and it is... but it's returning my local time still. I also checked that it's not defaulting to a simple time() because it failed to get the GMT time.

I've been using epochconverter.com to give me a sanity check with all these time stamps I'm getting.

Has anyone dealt with this before or have a clue what's going on? This has been causing me a lot of grief and I have an older app built using the now() function to record all dates in GMT... but now it looks like is recording everything in local time? I'm going to have to go double check that too now.
#2

[eluser]danmontgomery[/eluser]
Have you set your timezone, so PHP knows what GMT is? http://php.net/manual/en/function.date-d...ne-set.php?
#3

[eluser]Drew J[/eluser]
That was it! I set: date_default_timezone_set('UTC'); and it's giving me the correct time.

What's the reason that UTC time might not be UTC time? Is this something that should be submitted as a bug for CodeIgniter? It seems like UTC time is only one time, independent of timezones. gmdate() gives me the correct UTC time regardless of PHP settings.
#4

[eluser]Kevin Smith[/eluser]
Does anyone have any more insight into this? Messing with date/times in CI is always one of my least favorite things. I've run these same tests that Drew and others on the boards have to try to square up what's happening. It drives me mad!

time() always returns the proper unix timestamp, from what I can tell. [b]now()[/b] doesn't, and it seems like it's over-compensating. Here are the scenarios:

- If PHP has my timezone set for America/Chicago and I have my CI time_reference set to 'local', then now() outputs a correct Unix timestamp.
- If PHP has my timezone set for America/Chicago and I have my CI time_reference set to 'gmt', then now() outputs an incorrect Unix timestamp that is what the actual seconds since the unix epoch will be in exactly 5 hours.

- If PHP has my timezone set for UTC and I have my CI time_reference set to 'local', then now() outputs a correct Unix timestamp.
- If PHP has my timezone set for UTC and I have my CI time_reference set to 'gmt', then now() outputs a correct Unix timestamp.

time() is always right.
now() is usually right.

So to sum it up, I guess I don't see the point in using now().
#5

[eluser]Kevin Smith[/eluser]
Alright, did some more research, and to the best of my knowledge, this is what's happening:

In the 'system/helpers/date_helper.php' file on line 47 (as well as system/libraries/Session.php line 623), the timestamp is being formed by several gmdate() functions combined within a mktime() function. The problem with this: gmdate() outputs a GMT/UTC-based date/time, and mktime() does as well. Therefore, we're doubling our efforts here and converting to UTC twice. That would certainly explain why now() output a timestamp 5 hours ahead of the actual UTC time (for me since I'm in America/Chicago).

Instead of using mktime(), CodeIgniter should be using gmmktime() since it expects to receive GMT/UTC values. When I changed line 47 to reflect this, the problems noted in my previous post vanished. Now, that said...

I STILL DO NOT UNDERSTAND THE POINT OF THE now() FUNCTION. Was there a time or are there certain installations of PHP where time() will return a Unix timestamp referencing the timezone set for PHP? That's the only reason I can think of that would explain why this function exists.

Maybe those who have been around longer can shed some light on this.
#6

[eluser]Drew J[/eluser]
Hi Kevin,

Looks like you might be shedding some more light on the issue.

I opened a ticket here for the problem: https://bitbucket.org/ellislab/codeignit...-even-when

It's been 5 months and it looks like not many other people are experiencing (or realizing they are experiencing) the issue... it's still listed as "new".

I'm not sure what else to do except hope someone involved in the code experiences the same thing also.
#7

[eluser]Kevin Smith[/eluser]
Issue filed in CodeIgniter Reactor repo

Oh! Glad to see someone else filed an issue as well. I searched the Reactor repo for an issue on it and there was nothing. That early in the AM, I guess I didn't think to look in the original CI repo.

I'm pleased to have some validation in this. It felt like one of those things that might have been hashed out already time and again with others in the forums, the answer something simple that each one of us was missing. I couldn't find anything to that effect though. Just years of folks asking, "Why am I not getting the right date again?"

Let's hope one of the Reactor Engineers sees this. Smart dudes, that bunch.
#8

[eluser]Drew J[/eluser]
Yeah, that's good that you filed under the Reactor repo. I filed way back when, so you probably have a better chance now of getting some attention to it.




Theme © iAndrew 2016 - Forum software by © MyBB