Reputation: 152
I want to convert from UTC time to actual user timezone. realized through PHP can not do that. So I combined jQuery and PHP together.
I am attaching the code I wrote. Unfortunately something wrong but i dont know what and where is the problem.
Thanks in advance.
if(isset($_SESSION['timezone'])){
} else if(isset($_REQUEST['hiddenval'])) {
$_SESSION['timezone'] = $_REQUEST['hiddenval'];
header('Location: ' . $url);
} else {
echo '<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="//pellepim.bitbucket.org/jstz/jstz.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
var timezone = jstz.determine_timezone();
document.getElementById("hiddenVal").value = timezone.name();
</script>';
}
echo $_SESSION['timezone'];
source: http://pellepim.bitbucket.org/jstz/
Upvotes: 1
Views: 859
Reputation: 152
via SESSION:
<?php
session_start();
if(isset($_SESSION['timezone'])){
echo 'User timezone: ' . $_SESSION['timezone'];
} else if(isset($_REQUEST['timezone'])) {
$_SESSION['timezone'] = $_REQUEST['timezone'];
header('Location: ' . $_SERVER['PHP_SELF']);
} else {
echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?timezone="+Intl.DateTimeFormat().resolvedOptions().timeZone;</script>';
}
?>
Upvotes: 0
Reputation: 6638
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
$.ajax()
on $(document).ready
to send a correct timezone value to your PHP file.session_start()
on your PHP first line.Reference Documentation
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
Upvotes: 1