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/

Em Fhal
  • 152
  • 12

2 Answers2

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)
Community
  • 1
  • 1
Mohit Tanwani
  • 6,608
  • 2
  • 14
  • 32
  • 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 – Em Fhal Oct 09 '16 at 09:01
  • 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? – Mohit Tanwani Oct 09 '16 at 09:07
  • 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. – Mohit Tanwani Oct 09 '16 at 09:09
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>';
}
?>
Em Fhal
  • 152
  • 12