Web Development

get users’ timezone using php and javascript

You might have not faced this problem if you have not work in any project which is very strict in timing. But projects like scheduling and other tasks like users’ subscription with payment must have this features enabled.

With PHP date function you will get the date time of server on which site is located. The only way to get user time is to use JavaScript.

But I suggest you to, if your site have registration required then best way is to ask user while registration as compulsory field. You can list various time zones in register page and save that in database. After this if user login to site then you can set default time zone for that session as per users’ selected time zone. You can set any specific time zone using PHP function date_default_timezone_set. This set the specified time zone for users.

List of supported time zones can be found here.

Basically users’ time zone is goes to client side, so we must use JavaScript for this.

Below is the script to get users’ time zone using PHP and JavaScript.

[cc lang=”javascript”]



[/cc]

But as per my point of view, it’s better to ask to the users if registration is mandatory in your project.

Shares:
  • joomladev
    joomladev
    September 17, 2010 at 3:45 am

    Very good article

    Reply
  • Claudio Copen
    January 19, 2011 at 8:04 pm

    I am very thankful to this topic because it really gives up to date information *`;

    Reply
  • Kyle Gavalchin
    June 16, 2011 at 7:12 am

    Very Helpful…Thank You Soo Much! :D A++

    Reply
  • Artee
    Artee
    September 2, 2011 at 11:06 pm

    Just what I was looking for. Thank you very much

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *