set time locally

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

set time locally

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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
Post Reply