1

I have this piece of code

$timezone = 'America/New_York';
$now = new DateTime();
$now->setTimezone(new DateTimeZone('America/New_York'));
echo $now->format('Y-m-d h:i:s');

The above code works. But I want the $timezone variable to replace 'America/New_York'.

This does not work...

$now->setTimezone(new DateTimeZone($timezone));

This also does not work...

$now->setTimezone(new DateTimeZone("$timezone"));

Please help me solve this problem, I would greatly appreciate it. Thanks.

  • It works for me. https://3v4l.org/FRDCU What happens when you try? – aynber Oct 22 '19 at 17:21
  • "This page isn't working" – Donald Faulknor Oct 22 '19 at 17:36
  • That's a 500 server error (probably) and covers a multitude of issues. Check your server error logs to find out what the exact error is. – aynber Oct 22 '19 at 17:36
  • Error log says unknown or bad timezone. – Donald Faulknor Oct 22 '19 at 17:46
  • Only when attempting to use `$timezone = 'America/New_York'` but not when using `$timezone = 'America/Chicago'`. – Donald Faulknor Oct 22 '19 at 17:47
  • Very odd. If you have Chicago, you should have New York. Try `var_dump(timezone_identifiers_list());` to see what it all contains, and make sure that `America/New_York` is listed. – aynber Oct 22 '19 at 17:52
  • You can also try `var_dump( timezone_identifiers_list(DateTimezone::AMERICA));` or `var_dump(DateTimeZone::listIdentifiers(DateTimezone::AMERICA));` to just get the America zones, or `var_dump(DateTimeZone::listIdentifiers(DateTimezone::PER_COUNTRY, 'US'));` to get only the US ones. – aynber Oct 22 '19 at 17:55
  • I think I pinpointed it to how I retrieve the timezone, through JavaScript. `$timezone = '';` The value `$timezone` is definitely `America/New_York`, but it fails when I enter $timezone. – Donald Faulknor Oct 22 '19 at 17:57
  • That's the javascript timezones, not what PHP is seeing. – aynber Oct 22 '19 at 17:58
  • Then why can it be echoed through php. If I do this... `echo $timezone;` it will print America/New_York. – Donald Faulknor Oct 22 '19 at 18:00
  • You need to try one of the functions I posted above to make sure PHP is seeing what's installed on the system. Also `var_dump($timezone);` to make sure it looks correct and that there are no extra characters – aynber Oct 22 '19 at 18:01
  • string(132) "America/New_York" – Donald Faulknor Oct 22 '19 at 18:03
  • Well, there's your problem. `"America/New_York"` is not supposed to be 132 characters. There's something weird being passed in there. – aynber Oct 22 '19 at 18:06
  • Yea, I see that now. I'll try to remember to use var_dump next time. Is there another solution? I am trying to retrieve the user's local timezone in order to display the appropriate time. I've looked at other posts and couldn't seem to find the one that worked. – Donald Faulknor Oct 22 '19 at 18:07
  • That solution should be correct, but you're going to need to figure out why the timezone being passed is not the correct length. Look for hidden characters and try to trim them out. – aynber Oct 22 '19 at 18:09
  • The JavaScript script is exactly 132 characters. So, it's passing the entire script into the variable, even though, on output is only showing the timezone. – Donald Faulknor Oct 22 '19 at 18:09
  • The solution I found was to create a cookie. Thanks for the help. – Donald Faulknor Oct 22 '19 at 20:55

0 Answers0