11

As the title says, can anybody explain why the code

ini_set( 'date.timezone', 'Europe/Athens' );

works but

date_default_timezone_set( 'Europe/Athens' );

does not? It gives me this error:

Fatal error: Call to undefined function date_default_timezone_set()

I was looking for a solution to convert a timestamp and all I could find on the web was to use date_default_timezone_set. But the conversion had always 1 hour difference.

Then I found this topic "strftime() function showing incorrect time" which is using the init_set code (and afterwards I've RTM and found out that it's already mentioned in the documentation). I tried it and it worked.

But I haven't found an answer why date_default_timezone_set is not working.

Can anybody explain please?

miken32
  • 42,008
  • 16
  • 111
  • 154
  • are timastamps not UTC and Athens would be +1? maybe try Europe/Dublin – KevInSol Feb 26 '13 at 18:45
  • Please share the full code.. – Evert Feb 26 '13 at 20:04
  • Working code http://pastebin.com/EQx9jeGm – Charalampos Anargyrou Feb 26 '13 at 20:22
  • Non working code http://pastebin.com/c9HpwTF5 – Charalampos Anargyrou Feb 26 '13 at 20:22
  • @user602088 sorry but I don't understand your comment – Charalampos Anargyrou Feb 26 '13 at 20:23
  • @CharalamposAnargyrou Do you have errors, warnings, and notices turned on? There's a chance the error log will tell you a bit more. Also, what is the value of `$item->pubDate`? Additionally, in your non-working code, I didn't notice a call to `date_default_timezone_set()`... where is that being called? – 0b10011 May 14 '14 at 17:10
  • It sounds like you just want to convert from UTC to a specific time zone, [which is covered in this answer](http://stackoverflow.com/a/5746606/634824). Leave "defaults" out of it if you can. – Matt Johnson-Pint May 14 '14 at 18:46
  • I would concur with Matt Johnson. I would avoid changing the default timezone unless you are actually correcting for something (ie, you want your script [that is in Australia] to execute in your local timezone [say, Athens]). If all you want to do is convert a timestamps, there are other ways. – Mike May 14 '14 at 21:21
  • what server software do you use? OS? – Arie May 14 '14 at 22:18

3 Answers3

3

You must be using an old version of PHP.

date_default_timezone_set is for PHP 5.1.2 and above.

Try php -v and check your PHP version.

0

You can use:

putenv("TZ=Europe/Berlin"); // PHP 4
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
adilbo
  • 910
  • 14
  • 22
-1

localhost

server

It's quite late, I got the same issue and I see there is the difference, I post the screenshot from my localhost and my personal server phpinfo for the timezone.

On my localhost, date_default_timezone_set('[timezone]') works correctly, however, on the server, it always return the UTC. Based on the php doc, it looks like the timezone is not set in the php ini of my server, so i likely ignored the the timezone set since no value is defined previously.

Using the init_set, it makes the timezone to be available. I think the timezone is not predefined on the php ini is the cause.

Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39