0

I am writing some code that requires the timezone to be set. I'm using date_default_timezone_set() but I am hardcoding the parameter to a specific city. I do not want to do this because other people will potentially run this code in other timezones or cities or states. Is there a way to dynamically set it somehow so I do not have to hardcode the value?

I do not want the user's timezone. I specifically want to be able to find the server's timezone that its running in.

Sherif
  • 11,786
  • 3
  • 32
  • 57
Big Data Brian
  • 123
  • 1
  • 10
  • You cannot. Since PHP is server-side it expects server time. If you want client-side time you have to use JavaScript. – Jay Blanchard Mar 08 '17 at 19:11
  • I don't care what the user's time zone is, I want the server's time zone. Basically I want to find out what timezone the PHP server code is running in because it could be running in a server on the east coast or west coast. – Big Data Brian Mar 08 '17 at 19:12
  • You will have to set the timezone in each server. – Jay Blanchard Mar 08 '17 at 19:13
  • 3
    A suggestion I've seen many times would be to use a constant frame of reference (e.g. UTC) for storing times (or even just generating them on the server). Convert to a local time whenever you need to actually display that time to a user. – Jeff Lambert Mar 08 '17 at 19:17
  • Do I have to do that in the php.ini file or can I do a shell_exec("date +%Z") and use that to set the timezone? For instance, I can get that to return "MST" for Mountain Standard Time, but I don't see that as a valid option for a parameter to PHP's date_default_timezone_set() function. – Big Data Brian Mar 08 '17 at 19:18
  • I probably should've mentioned, I'm only setting it if date_default_timezone_get() returns null. – Big Data Brian Mar 08 '17 at 19:19
  • Yes, the php.ini file should contain the proper timezone setting. Also, date_default_timezone_get() doesn't return null. It returns UTC timezone by default if no other timezones are found. If the timezone is set properly in the php.ini, you won't have to use date_default_timezone_set() – Walker Boh Mar 08 '17 at 20:04
  • Please read [the tz/dst best practices article](http://stackoverflow.com/q/2532729/634824). Specifically, note that expecting the server time zone to be anything in particular is not a good idea. Your code should be completely independent of any OS or platform settings. Keep PHP set to UTC, and only ever ask for the UTC time. If you want time in a different time zone, ask *explicitly* for the time in that time zone. This applies across all languages and all platforms. – Matt Johnson-Pint Mar 09 '17 at 05:10

2 Answers2

1

Use date_default_timezone_get() to get the servers timezone. If the server has the timezone configured properly in the php.ini, you won't need to set the timezone at all. Nor should you. It should be up to the server admins to correctly configure their ini settings.

Walker Boh
  • 750
  • 6
  • 13
0

Just don't set the timezone in your PHP code, and use the server configuration. Otherwise you're overwriting the server setting. The php.ini has a directive for timezone:

date.timezone string

The default timezone used by all date/time functions. Prior to PHP 5.4.0, this would only work if the TZ environment variable was not set. The precedence order for which timezone is used if none is explicitly mentioned is described in the date_default_timezone_get() page. See List of Supported Timezones for a list of supported timezones.

It's up to the users of your code to have the server configured properly.

sidyll
  • 57,726
  • 14
  • 108
  • 151
  • 1
    You make a good point. It should be up to the people deploying the code to properly set the timezone. I just wanted to try to do as much as I can in-code to get it to work without having to say "Go configure your server!" – Big Data Brian Mar 08 '17 at 19:27
  • @BigDataBrian completely agreed! You're already being too nice in my opinion :D but a warning if it is not configured is an excellent solution. – sidyll Mar 08 '17 at 19:37