Search code examples
phpdatetimezonesetdefault

Set the default time zone for the PHP application


I am wondering if we can also use the init_set() function to set the default time.

  • a- I know that that we can set the timezone zone in editing the php.ini.
  • b- Using the Linux time() command on PHP.
  • c- init_set('date.timezone', 'Europe/Edinburgh');

The reason that I asked it was one other questions that I come across in a book. And the answer is only A.

Is the book wrong?

It should be A and C right? Thanks


Solution

  • There is also option D: use date_default_timezone_set. The function you refer to in C is actually ini_set. So the three ways are:

    1. add a line in php.ini:

      date.timezone = "Europe/Edinburgh"

    2. use ini_set:

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

    3. use date_default_timezone_set:

      date_default_timezone_set('Europe/Edinburgh');

    The thing to note about options 2&3 is that they have to be called every time you run a script. So if you need to change the timezone for every script you run, it is easier to use option 1 (assuming you have edit access to php.ini).

    As for option B, I guess in theory it could work but do you really want to play with your system's time? (hint: no).