By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

Time Zone Problem

P: n/a
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($timestamp,$from_tz,$to_tz) {
$old_tz = getenv('TZ');
// Parse $timestamp and extract the Unix time.
if(!empty($from_tz))
putenv("TZ=$from_tz");
$unix_time = strtotime($timestamp);
// 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($timestamp_with_timezone,$to_tz) {
// Set $from_tz to blank, so that $timestamp_with_timezone
// is parsed to determine timezone.
return tz_to_tz($timestamp_with_timezone,'',$to_tz);
}

/** Converts a timestamp with timezone information into UTC. */
function to_utc($timestamp_with_timezone) {
return to_tz($timestamp_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;

?>
Oct 14 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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: ch****************@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

iD8DBQFI9F3Xhbef5VWlKioRAqvkAJ9nDKVxpbsmqF/k5E+HIqIcI4aNoQCcDfwe
Vace8POwhjBKwXWmxs/aRPc=
=rxqm
-----END PGP SIGNATURE-----

Oct 14 '08 #2

P: n/a
On Oct 14, 1:52*pm, Christoph Burschka <christoph.bursc...@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_timezone_set('UTC');
$utc_time = date("Y-m-d H:i:s");
echo "UTC Time is ::".$utc_time;

-Rahul
Oct 14 '08 #3

P: n/a
On Tue, 14 Oct 2008 10:38:19 +0200, R*a*h*u*L <ra******@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($timestamp,$from_tz,$to_tz)
<?php
date_default_timezone_set('America/Denver');
function tz_to_tz($timestamp, $from, $to){
$from_tz = new DateTimeZone($from);
$to_tz = new DateTimeZone($to);
$original = new DateTime("@$timestamp",$from_tz);
$target = new DateTime("@$timestamp",$to_tz);
$offset = $to_tz->getOffset($target) - $from_tz->getOffset($original);
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,'America/Denver','UTC'));
?>
--
Rik Wasmus
Oct 21 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.