开发者

how to show time and date according to user client (in lamp)

开发者 https://www.devze.com 2022-12-16 03:38 出处:网络
how in lamp (LINUX ,APACHE ,MYSQL ,PHP) environme开发者_如何学Gont , can i show date and time in web page according to the user time zone ?

how in lamp (LINUX ,APACHE ,MYSQL ,PHP) environme开发者_如何学Gont , can i show date and time in web page according to the user time zone ?

THANKS


Use JavaScript to determine the timezone of the user:

new Date().gettimezoneOffset()


On the PHP side this code works fine...

  echo <<<EOE
   <script type="text/javascript">
     if (navigator.cookieEnabled)
       document.cookie = "tzo="+ (- new Date().getTimezoneOffset());
   </script>
EOE;
  if (!isset($_COOKIE['tzo'])) {
    echo <<<EOE
      <script type="text/javascript">
        if (navigator.cookieEnabled) document.reload();
        else alert("Cookies must be enabled!");
      </script>
EOE;
    die();
  }
  $ts = new DateTime('now', new DateTimeZone('GMT'));
  $ts->add(DateInterval::createFromDateString($_COOKIE['tzo'].' minutes'));
0

精彩评论

暂无评论...
验证码 换一张
取 消