1

I want to have the ability to set the timezone based off of a variable. I have the following:

$timezone_set = 'America/New_York';
date_default_timezone_set($timezone_set);
ini_set("date.timezone", $timezone_set);

echo(date_default_timezone_get());
echo($timezone_set);

This outputs:

UTC
America/New_York

However, if I use:

$timezone_set = 'America/New_York';
date_default_timezone_set("America/New_York");
ini_set("date.timezone", "America/New_York");

echo(date_default_timezone_get());
echo($timezone_set);

I get:

America/New_York
America/New_York

Can someone explain a) if what I'm trying to do is possible b) if so, how?

thebarless
  • 470
  • 1
  • 12
  • 34
  • 1
    I can't reproduce: http://ideone.com/HxraCP – Barmar Nov 23 '15 at 16:40
  • 1
    check the return value of the date_default call, it'll be boolean false on failure - e.g. incorrect tz string. – Marc B Nov 23 '15 at 16:41
  • 1
    Are you getting the timezone variable from user input or a file? I suspect there's some non-printing characters in it, like an extra space or newline, and that's messing up `date_default_timezone_set`. – Barmar Nov 23 '15 at 16:46
  • 1
    I also can't reproduce – iam-decoder Nov 23 '15 at 16:46
  • 1
    @thebarless it still isn't causing problems for me, even with single quotes. There's probably something else that's causing your issue that we can't see. – iam-decoder Nov 23 '15 at 16:55
  • @iam-decoder I get the following `PHP Notice: date_default_timezone_set(): Timezone ID 'America/New York' is invalid in... on line 67` from the error log. Once I switched the quotes, it began evaluating without the notice. – thebarless Nov 23 '15 at 16:59
  • so, on your server when you use single quotes, underscores are removed? – iam-decoder Nov 23 '15 at 16:59
  • I didn't even notice that - it appears so. I'll have to look into it a little deeper because that seems to be odd behavior. – thebarless Nov 23 '15 at 17:05
  • @iam-decoder, I reverted to the prior version that wasn't working originally - pre-quote change - and that now works. Prior to posting, I had dumped browser caches and repro'd from three separate clients to the production and testing server with the same results. Any thoughts of where I should look? It seems you are correct that the quotes didn't do it. – thebarless Nov 23 '15 at 17:11
  • 1
    it's definitely something going on in php not in your Apache/IIS. But the only thing I can think is that some thing is processing your script prior to evaluating it. – iam-decoder Nov 23 '15 at 17:12
  • See [here](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) for the differences between single and double quotes. I've never heard of them treating underscore differently, I suspect the problem is with something else. – Barmar Nov 23 '15 at 17:23

0 Answers0