3

I got a strange problem with Cake. I live in Germany. When I add posts the time in the "created" database-field is 6 hours back. E.g. a post created 6pm shows up in the database as created 12am. I use a managed server and I contacted the company which set the php.ini to the correct timezone Europe/Berlin. When I request the servers php.ini, it shows the timezone "Europe/Berlin" correctly. But when I run the code

$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;

in CakePHP it shows

The current server timezone is: America/New_York

I already tried to set in the core.php:

date_default_timezone_set('Europe/Berlin');

But that has no effect at all. Hopefully someone knows what to do.

EDIT 1:

After asking the support to run "locale -a", following was spit out:

# locale -a
C
de_DE
de_DE@euro
de_DE.iso88591
de_DE.iso885915@euro
de_DE.utf8
deutsch
en_US.utf8
german
POSIX

I assume that serverside everything is fine and the error is cake-wise. But I don´t have a clue what to do next? Maybe someone can help. Thanks in advance.

** EDIT 2: **

After looking around what it could be I found the error in the bootstrap.php of the Usermanagement-plugin, which set the timezone there to UTC. So everything works fine now. Thanks for your help anyway.

Karl
  • 410
  • 11
  • 25

3 Answers3

5

Edit:

If doing the answer below has no effect (per OP), it could be this issue:

Is is quite likely that the German locale is not installed on the server your running the script on - do you have shell access to the server? Then try

locale -a

to see which locales are installed. Also have a look here Is it feasible to rely on setlocale, and rely on locales being installed?


TLDR:

You had the right idea - just put it in your Config/bootstrap.php. instead of your Config/core.php.

More Detail:

This is likely the same as this question. It's answer:

Put this in your Config/bootstrap.php:

date_default_timezone_set('UTC'); //or whatever your timezone is

It's just based on the server time and really has nothing to do with CakePHP - so just change the default timezone with PHP, and you should be good to go. 'created' and 'modified' will be based on the specified timezone.

To reiterate, the "created" and "modified" fields ARE based on the server and really don't have much to do with CakePHP (which it sounds like you already assumed correctly).

A related item (just fyi to read up on) is CakeTime::convert(), but in this case, is not what you'd want.

Community
  • 1
  • 1
Dave
  • 28,833
  • 23
  • 113
  • 183
  • I´m sorry but there´s no effect :-( – Karl Feb 25 '13 at 18:08
  • @edit of Dave. Sadly I don´t have shell access but I will ask the support, if they´d do that for me. Thanks for your great help :-) – Karl Feb 25 '13 at 18:16
  • I´m back. The support-guy ran the command and the following was spit out: # locale -a C de_DE de_DE@euro de_DE.iso88591 de_DE.iso885915@euro de_DE.utf8 deutsch en_US.utf8 german POSIX. So what to do now? I don´t have a clue at all – Karl Feb 26 '13 at 17:07
0

Set in app_controller not in code.php

Crsr
  • 624
  • 3
  • 9
  • No, it´s 2.x. I´ll try Daves ideas. Thank you anyway :-) – Karl Feb 25 '13 at 18:17
  • Well, it shows the correct timezone in cake but the saved posts are still the wrong time. That lets assume the server has some issues with locale. I´ll come back and tell the response of the server-company. – Karl Feb 25 '13 at 18:30
0

Problem solved. The problem was in a plugin which also had a config.php. There the Cake-config was overwritten. Now everything is fine.

Karl
  • 410
  • 11
  • 25