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
Array to Object and Object to Array in PHP - Interesting Behaviour
Getting a PHP Pdo Connection from a MySQL_Connect()
PHP Run Once and Insert Twice in MySQL Database
Laravel Livewire Component Not Refreshing/Reloading Automatically After Refreshing It
MySQL Encryption/Storing Sensitive Data,
Leverage Browser Caching, How on Apache or .Htaccess
How to Inject a Repository into a Service in Symfony
Point in Polygon Algorithm Giving Wrong Results Sometimes
Mysqli_Select_Db() Expects Parameter 1 to Be MySQLi, String Given
How to Determine a File's True Extension/Type Programmatically
Interface Implementation: Declaration Must Be Compatible
PHP - Add Link to a Url in a String
Is It a Good Idea to Use $_Server['Document_Root'] in Includes
Ftp_Put Is Corrupting My Movies After Transfer, Is It Wrong