0

I want to set default timezone to the user's local timezone, please find my code below:

date_default_timezone_set("Asia/Karachi");
$currentHour = date('h:i');

My code works correctly, but only works only for Karachi; if user in other country it may not works.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ehsan Ilahi
  • 298
  • 5
  • 15

2 Answers2

1

You can't get a user's timezone on the server side, and you can only make a guess at it on the client side.

If you rely on getting the user's IP address then you could geolocate that and deduce a time.

The way this is usually done is by asking the user (when they register, for instance) what timezone they are in and then use this in your time calculations.

Or use this javascript solution

http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/

NID
  • 3,238
  • 1
  • 17
  • 28
0

If you want to make timezone global and not fix to a single timzone then I guess you should use UTC timezone and there are two part of this one is for America and one for rest of the world.

Hope this link will work. Click here to know more

M K Garwa
  • 495
  • 2
  • 13