Web Hosting Forums

Results 1 to 4 of 4

This is a discussion on PHP->Show local time in the Hosting Talk & Chit-chat forum
On my page, times are being displayed as the times on the server. I have added a new option so that a user can select ...

  1. #1
    Chimp Cralis's Avatar
    Join Date
    Jul 2006
    Location
    Dunfermline, Scotland
    Posts
    13

    PHP->Show local time

    On my page, times are being displayed as the times on the server. I have added a new option so that a user can select their timezone. However, anyone have a function that I can pass an offset in say, minutes, and it returns a date/time based on that offset?

    Eg:

    funcShowTime(+60); //

    Would return a string showing the date/time for GMT+1?

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    PHP reports time in seconds since the epoch (Jan 1, 1970). All times are returned as an intger unless you use a formatting function, like date() to convert it to something more user friendly.

    The servers are set to US central time (-0600), although the servers in the Atlanta NOC may be at -0500. So to get the time in GMT you would just subtract 21600 (6 hrs * 60 min/hr * 60 sec/min) from a timestamp returnd from the server. This doesn't account for daylight saving time, but you can test for that with date('I'), which will return a "1" if the server is in DST or a "0" if it isn't.

    PHP5 introduces a date_default_timezone_set() function that will let you set the timezone that you want the time functions to use in your scripts, but if you are using PHP4 then you need to do it manually.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    Chimp Cralis's Avatar
    Join Date
    Jul 2006
    Location
    Dunfermline, Scotland
    Posts
    13
    Thanks for the excellent response.

    So, I can use something like this:

    function show_date ( $offset, $date, $time ) // $date and $time are booleans
    {
    $mytime = gmmktime () + ($offset * 60); // Offset is in minutes.
    if $date then return only a formatted date
    if $time then return only a formatted time
    if both then return only a formatted date and time.



    }

  4. #4
    Chimp Cralis's Avatar
    Join Date
    Jul 2006
    Location
    Dunfermline, Scotland
    Posts
    13
    Here is my function, but no matter what I put in the $offset parameter, the time stays the same.

    Anyway ideas?

    function show_date ( $offset, $dst, $date, $time )
    {
    $mytime = (gmmktime () + $offset);

    if ($date) {
    $formatted = date("m.d.y", $mytime);
    }
    if ($time) {
    $formatted = $formatted . ' ' . date("H:i:s", $mytime);
    }

    return trim( $formatted );
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •