0

I wrote this function to change the times displayed on my site based on the users timezone

function userTime($time, $timezone) {
    $datetime = new DateTime($time, new DateTimeZone($timezone));
    return $datetime->format("Y-m-d H:i:s");
}

echo userTime(date('Y-m-d H:i:s'), "America/New_ York");

The function works but to where it's spitting out a different time from the users actual location, am I doing something wrong or am I missing a step?

ruinher
  • 9
  • 3
  • What part of this code would make you think it knows the user's location? (Also, you have an extraneous space after the underscore in the time zone id). – Matt Johnson-Pint Sep 08 '15 at 15:42
  • That's actually an accidental space from when I typed this post, the code is normal. I use the GeoIP to get the users timezone. – ruinher Sep 08 '15 at 16:00
  • See the dup post for how to do it. You need to consider the source time zone, and you need to first create a `DateTime` in that source time zone before calling `setTimezone` to convert it to a different time zone. Your current code *creates* the value in the destination time zone, rather than converting to it. – Matt Johnson-Pint Sep 08 '15 at 16:06

0 Answers0