1

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/

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

hi, thank you for your full answer. Can you pls add to your answer what i need to do if i want using SESSION and not cookie. TIA
Just check you have session_start() in your first line of php file, your getting proper value from your js and session is setting properly before redirect?
Just go step by step to debug ur code, are you getting timezone right and it's setting properly on hidden variable or I would suggest to use Ajax call to get the timezone value and set in session when page is ready.
0

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>';
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.