Page 2 of 2

Posted: Tue Mar 22, 2005 7:11 am
by Chris Corbyn
There's a built in function.... easy as 1-2-3 :wink:

Code: Select all

echo gmdate("d-M-Y H:i:s");
EDIT: Ermmm... maybe I've misunderstood your question. I see n00b used this in his script.

Posted: Tue Mar 22, 2005 7:14 am
by Pyrite
I'm afraid not, that only gives the date of the server's locale.

Posted: Tue Mar 22, 2005 7:23 am
by n00b Saibot
Hmmm. i dunno why its so because it give me correct output here.

Code: Select all

echo date("d-M-Y H:i:s",returnLocalTime("-0700"));
returns 22-Mar-2005 06:23:55.


feyd | :grumble:

Posted: Tue Mar 22, 2005 7:31 am
by Pyrite
It's because your GMT is different than mine. To code this properly, I have to constantly keep switching timezones on my localhost. I've almost got it though, give me a few more min.

Posted: Tue Mar 22, 2005 8:13 am
by Pyrite
I think i got it, give it a test please.

Code: Select all

<?php

function is_positive($foo) {
	if ($foo > 0) {
		return true;
	} else {
		return false;
	}
}

// Inputs: $target_gmt Must Be Between -12 to +13
// Output: Date/Time of Target GMT
function get_worldtime($target_gmt) {

	$local_gmt = substr(date("O"),0,3); // eg. +07 (Thailand)

	if (!is_positive($local_gmt) && !is_positive($target_gmt)) {
	// Both Are Negative
		if ($target_gmt < $local_gmt) {
		$time_diff = substr($target_gmt,1,2)-substr($local_gmt,1,2);
		$time_diff = "-".$time_diff;
		} else {
		$time_diff = substr($local_gmt,1,2)-substr($target_gmt,1,2);
		}
	}
	elseif (!is_positive($local_gmt) || !is_positive($target_gmt)) {
		// Only One is Negative, Add Them
		$time_diff = substr($target_gmt,1,2)+substr($local_gmt,1,2);
		if (!is_positive($target_gmt)) {
			$time_diff = "-".$time_diff;
		}
	} 
	elseif (is_positive($target_gmt) && is_positive($local_gmt)) {
		// Both are postive, subtract them
		$time_diff = $target_gmt-$local_gmt;
	}

	$stamp  = (time() + ($time_diff*60)*60); // Target Timestamp
	return date("F j, Y @ g:i A", $stamp);
}

echo get_worldtime("+10")."<br>"; // Sydney, AU
echo get_worldtime("+02")."<br>"; // Amman, JO
echo get_worldtime("-07")."<br>"; // USA MST
echo get_worldtime("-05")."<br>"; // USA EST


?>

Posted: Tue Mar 22, 2005 4:58 pm
by Pyrite
Well, the above function seems to work anywhere, but it doesn't take into account DST/Summer Time, and I don't think it is possible to calculate that, since DST is sometimes difference for every country every year.

I found a great way to do this in perl, but using ENV vars and this calculates DST:

Code: Select all

#!/usr/bin/perl
use DateTime;
my $dt2 = DateTime-&gt;from_epoch( epoch =&gt; time() );
$dt2-&gt;set_time_zone('America/Denver');
print $dt2-&gt;hms;
But this doesn't work on all servers, because the env vars are different across OS's.

PHP can do the same however as well, but once again, not always using the same timezone names.

Code: Select all

<?php
putenv("TZ=US/Mountain");
echo date("M d Y H:i:s");
?>
Both of these methods handle DST correctly, but are given by TZ names, and are not crossplatform, rather, different on diff platforms.