How to display time local to a user

Discussion in 'PHP' started by virtualmisc, Feb 10, 2010.

  1. #1
    Hello

    At sign-up, I collect the user's country and set the nearest timezone (they can change this if it is incorrect).

    I want to display time local to a user. How can this be done?


    Thanks
     
    virtualmisc, Feb 10, 2010 IP
  2. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #2
    this is how I personally do it. it just gives you a brief on the calculations, you can do it your own way

    $t_ime = the timestamp ( time() ) stored in the db
    so if you want just the current time then $t_ime = time();

    $this->tzone
    is a number I believe
    since times are set at GMT0

    for example
    GMT+1, tzone would be 1
    or GMT-10, tzone would be -10

    and eh, I'm not too sure what else to say really. I wrote this three years ago when I was 15... but it's always worked for me


    edit

    
    //set this to whatever timezone the user is in. for example -10, 11, -3 ect..
    $Tz = -10;
    
    //set timezone
    date_default_timezone_set('Europe/London');
    
    //Current server time
    $Current = time();
    
    //Work out
    $add = ($Tz * 3600);
    
    //Work out timezone
    $Current = $Current + $add;
    
    //show local time
    $result = date('M d, Y H:i', $Current);
    
    Code (markup):
     
    Last edited: Feb 10, 2010
    Meth_, Feb 10, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You have to use javascript or actually ask the user what their timezone is. There is no server side method of determining this, as the user's browser does not pass this information in a page request.

    You can also use a geoip database to get closer, but it's not exact.
     
    jestep, Feb 11, 2010 IP