Time Zone Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R*a*h*u*L

    Time Zone Problem

    Hi,

    Can anyone help me out .

    Req: I have to convert one of my server Date/Time (timezone) which is
    in America/Denver to UTC or GMT time zone .

    I have tried the below but both gives me same date/time pl help.

    <?php

    /** Converts a timestamp between arbitrary timezones. */
    function tz_to_tz($times tamp,$from_tz,$ to_tz) {
    $old_tz = getenv('TZ');
    // Parse $timestamp and extract the Unix time.
    if(!empty($from _tz))
    putenv("TZ=$fro m_tz");
    $unix_time = strtotime($time stamp);
    // Unix time is seconds since the epoch (in UTC).

    // Express the Unix time as a string for timezone $tz.
    putenv("TZ=$to_ tz");
    $result = strftime("%Y-%m-%d %H:%M %Z",$unix_time) ;
    putenv("TZ=$old _tz");
    return $result;
    }

    /** Converts a timestamp with timezone information into
    * an arbitrary timezone. */
    function to_tz($timestam p_with_timezone ,$to_tz) {
    // Set $from_tz to blank, so that $timestamp_with _timezone
    // is parsed to determine timezone.
    return tz_to_tz($times tamp_with_timez one,'',$to_tz);
    }

    /** Converts a timestamp with timezone information into UTC. */
    function to_utc($timesta mp_with_timezon e) {
    return to_tz($timestam p_with_timezone ,'UTC');
    }


    $now = date("Y-m-d H:i");
    $utc_time = tz_to_tz($now," America/Denver","UTC");

    echo "Local time is ::".date("Y-m-d H:i")."<br>";
    echo "UTC Time is ::".$utc_time ;



    ?>
  • Christoph Burschka

    #2
    Re: Time Zone Problem

    R*a*h*u*L wrote:

    $now = date("Y-m-d H:i");
    You may have your reasons for doing it like this, but I still have to ask: If
    you are going to convert the timestamp to Unix format anyway, why don't you call
    time()? time() will return a Unix timestamp (in UTC) wherever the server is.

    If you just store and use the Unix timestamp exclusively whenever passingaround
    internal values, you can easily adjust it to any timezone in the presentation layer.

    -Christoph

    --
    "Omniscient ? No, not I; but well-informed."
    ----------------------
    XMPP: christoph.bursc hka@gmail.com
    AOL: 313125838 / cburschka
    Key: 0x55A52A2A


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFI9F3Xhbe f5VWlKioRAqvkAJ 9nDKVxpbsmqF/k5E+HIqIcI4aNoQ CcDfwe
    Vace8POwhjBKwXW mxs/aRPc=
    =rxqm
    -----END PGP SIGNATURE-----

    Comment

    • R*a*h*u*L

      #3
      Re: Time Zone Problem

      On Oct 14, 1:52 pm, Christoph Burschka <christoph.burs c...@rwth-
      aachen.dewrote:
      R*a*h*u*L wrote:
      >
        $now = date("Y-m-d H:i");
      >
      You may have your reasons for doing it like this, but I still have to ask: If
      you are going to convert the timestamp to Unix format anyway, why don't you call
      time()? time() will return a Unix timestamp (in UTC) wherever the server is.
      >
      If you just store and use the Unix timestamp exclusively whenever passingaround
      internal values, you can easily adjust it to any timezone in the presentation layer.
      >
      -Christoph
      >
      --
      "Omniscient ? No, not I; but well-informed."
      ----------------------
      XMPP: christoph.bursc ...@gmail.com
      AOL:  313125838 / cburschka
      Key:  0x55A52A2A
      >
       signature.asc
      < 1KViewDownload
      Thanks Christoph.
      I will make a try of your suggestion.

      Well I have tried the below given and it works ...

      echo "Local time is ::".date("Y-m-d H:i:s")."<br>";
      date_default_ti mezone_set('UTC ');
      $utc_time = date("Y-m-d H:i:s");
      echo "UTC Time is ::".$utc_time ;

      -Rahul

      Comment

      • Rik Wasmus

        #4
        Re: Time Zone Problem

        On Tue, 14 Oct 2008 10:38:19 +0200, R*a*h*u*L <rahulazm@gmail .comwrote:
        Hi,
        >
        Can anyone help me out .
        >
        Req: I have to convert one of my server Date/Time (timezone) which is
        in America/Denver to UTC or GMT time zone .
        >
        I have tried the below but both gives me same date/time pl help.
        >
        <?php
        >
        /** Converts a timestamp between arbitrary timezones. */
        function tz_to_tz($times tamp,$from_tz,$ to_tz)
        <?php
        date_default_ti mezone_set('Ame rica/Denver');
        function tz_to_tz($times tamp, $from, $to){
        $from_tz = new DateTimeZone($f rom);
        $to_tz = new DateTimeZone($t o);
        $original = new DateTime("@$tim estamp",$from_t z);
        $target = new DateTime("@$tim estamp",$to_tz) ;
        $offset = $to_tz->getOffset($tar get) - $from_tz->getOffset($ori ginal);
        return $timestamp + $offset;
        }
        $check = time();
        echo 'Denver '.date('Y-m-d H:i:s',$check). ' to UTC '.date('Y-m-d
        H:i:s',tz_to_tz ($check,'Americ a/Denver','UTC')) ;
        ?>
        --
        Rik Wasmus

        Comment

        Working...