Wrong time problem in PHP (America/Sao_paulo)

Asked

Viewed 9,275 times

2

I’m running an application in a Centos production environment with updated PHP and Apache.

I realized that the logs Cakephp returns a wrong time from the set in php.ini and also set inside the application’s Bootstrap by date_default_timezone_set(..).

Both in the PHP logs and date the time returned is always 2 hours less.

For example, it is now 15:35 and php returns as:

20/01/2015 13:35:10 America/Sao_paulo BRST

The operating system date is in order, shows on the right date, executes the crons at the right time as well.

What may be happening?

  • 1

    What do you call "server time is in order"? That it is in UTC time, ie GMT, which is in universal time? Well, if so, the time shown is also correct. 15:45 GMT equals 13:35 BRST. Either you need to convert or use the actual server time on the TZ you want. You did not show used codes to indicate how you got the results you spoke so I can not detail more.

  • The code used is a simple PHP date() . The server is in GMT(Tue Jan 20 18:09:28 GMT 2015).. maybe that would be it?

  • Exactly, the server is on time, the return is on time, the only thing wrong is the interpretation of the time. If there’s time I’ll come up with an answer on this.

  • It would be good to inform what is the clock difference of the motherboard also, for comparison purposes (usually available when setting up the BIOS or equivalent).

  • 1

    PS: Not directly related to this problem, but virtualized machines can make a difference in minutes as well, depending on how clock timing is set up. (Normally this should be done by the virtualization environment instead of conventional NTP).

  • 1

    @Bacco that is the explanation for the error I speak at the end of my answer.

Show 1 more comment

2 answers

4


By every description of the problem I read I understand that there is only one misinterpretation of how to use the date.

My understanding is that the server is on time, at least your examples match the time of Brazil at the time you post. You are interpreting that the time is in GMT which is the universal time, but the server time is in the time Brazil\East, he’s using the -0200. GMT uses the 0000. How the server interprets that it is in GMT, any attempt to use another Timezone will currently result in 2 hours of error. So if the server time is already the time you want, you cannot use another Timezone other than the GMT.

Another possible interpretation is that the server time is wrong, since it is with Brazil time but is indicating that the time is GMT. In fact, strictly speaking, it is wrong. When you say precisely at 18:09 of the Brazilian time that your server shows 18:09 GMT indicates that it is wrong, because at 18:09 Brazilian time the correct GMT is 20:09.

You have some options:

  • If you want to use the Brazilian time on the server Timezone from the server to be BRST (you will need to adjust to BRT when summer time is over, if the server does not automatically).
  • If you want to keep the use of GMT, put the right time of the now, put the London time and not ours.
  • Or still leave it the way it is and stop configuring other software to use another Timezone, since what’s on the server is exactly the time you want. I mean, stop setting up PHP, whether in ini, be with the function date_default_timezone_set.

The latter solution is not the most correct one. If you have some software you cannot control the Timezone, will give problem. In compensation is the only form of poorly made software that does not consider the Timezone function properly.

See a server with everything correct (despite the time not being there so well synchronized at the time I tested :) ).

I put in the Github for future reference.

0

I don’t use Cakephp, but with a google query, question Stackoverflow in English that talks to set up Timezone in the archive config/core.php in the application folder.

  • I think he said he already does this.

  • @bigown I really don’t know how Cakephp works, however, in the question quoted (in English), the author says he has already set Timezone in the archive bootstrap.php and set in the file config/core.php solved the problem =)

  • I’ve already set up in core and all other possible ways, actually php ta playing my Timezone America/Sao_paulo with 2 hours less

Browser other questions tagged

You are not signed in. Login or sign up in order to post.