How to Get User Timezone Using Jquery

Getting the client's time zone (and offset) in JavaScript

Using getTimezoneOffset()

You can get the time zone offset in minutes like this:

var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01

Get timezone from users browser using moment(timezone).js

var timedifference = new Date().getTimezoneOffset();

This returns the difference from the clients timezone from UTC time.
You can then play around with it as you like.

Detect TIMEZONE with jQuery and combined with PHP

To get timezone in JavaScript, you should use.

Intl.DateTimeFormat().resolvedOptions().timeZone

Why you're trying to use hidden variable ?

1. Using Cookie

Javscript Code

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
}

createCookie('cookieee','client_timezone', Intl.DateTimeFormat().resolvedOptions().timeZone);

PHP Code

print_r($_COOKIE);

2. Using Session

  • You could use $.ajax() on $(document).ready to send a correct timezone value to your PHP file.
  • In PHP, if your session variable is not set then set it via Ajax request.
  • Don't forget to use session_start() on your PHP first line.

Reference Documentation

  • Intl.DateTimeFormat
  • Set cookie with JS and Read in PHP
  • Get user timezone in JS + PHP

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

get client time zone from browser

Look at this repository pageloom it is helpful

download jstz.min.js and add a function to your html page

<script language="javascript">
function getTimezoneName() {
timezone = jstz.determine()
return timezone.name();
}
</script>

and call this function from your display tag



Related Topics



Leave a reply



Submit