get GMT in string format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Last edited by Chris Corbyn on Tue Mar 22, 2005 7:15 am, edited 1 time in total.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

I'm afraid not, that only gives the date of the server's locale.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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:
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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


?>
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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.
Post Reply