Page 1 of 1

set time locally

Posted: Sat Dec 21, 2002 5:35 am
by jamal
hi Guys,
I am having problem in getting time locally set.
Please can anyone give me the function on how to define time locally. I hope my english is clearly understood.
I want the user to see the time on my site locally.
Please help me out.
God Bless You.

Posted: Sat Dec 21, 2002 10:36 am
by Gen-ik
You could just use JavaScript to display the time.. it will show the time that your visitors computer is set to.


If you need to do it in PHP the best way would be to get the time and/or date as GMT and then add or subtract the hours your country is in front of, or behind, GMT.

Using PHP to display your visitors local time is a bit more tricky as you would need to find out which country they are login-in from.. and then add/subtract the hours as needed.


Hope this helps.. it probably won't.. but hey ho.

Posted: Sat Dec 21, 2002 6:08 pm
by evilcoder

Code: Select all

<?php

session_start();


function set_timezone($offset) {
    if ($offset) {
        $offset = -$offset;
        $_SESSION['GMT_offset'] = 60 * $offset;
        $GMT_offset_str = ( $offset > 0 ) ? '+' : '-';
        $GMT_offset_str .= floor($offset / 60) . ':';
        $GMT_offset_str .= (($offset % 60) < 10 ) ? '0' . $offset % 60 : $offset % 60;
        $_SESSION['GMT_offset_str'] = $GMT_offset_str;
    }
}


function format_datetime($date) {
        return (date('j M Y g:ia', $date + $_SESSION['GMT_offset']) . ' GMT ' . $_SESSION['GMT_offset_str']);
}


function format_date($date) {
    return date('j M Y', $date);
}

if (!isset($_SESSION['GMT_offset']) ) {
    $_SESSION['GMT_offset'] = 0;
    $_SESSION['GMT_offset_str'] = '';
}


if (isset($_GET['offset']) ) {
    $_SESSION['offset'] = $_GET['offset'];
    set_timezone($_GET['offset']);
}


if ( !isset($_SESSION['offset']) ) {
?>
  <script type="text/javascript">
  window.onload = setLinks

  function setLinks() {
    now = new Date()
    offset = now.getTimezoneOffset();

    for ( i = 0; document.links.length > i; i++ ) {
      with ( document.links[i] ) {
        if ( href.indexOf('http://yoursite.com') == 0 ) {
          if ( href.indexOf('?') == -1 ) {
            href += '?offset=' + offset;
          } else {
            href += ';offset=' + offset;
          }
        }
      }
    }
  }
  </script>

<?php
}

?>

Parouse that. See if it works