Page 1 of 2

get GMT in string format

Posted: Tue Mar 22, 2005 2:44 am
by Pyrite
There a way to get the server's time in GMT string format.

ie. GMT+/-#, eg. GMT-7 or GMT+13

Posted: Tue Mar 22, 2005 2:55 am
by Chris Corbyn
PHP.net date() function wrote:O Difference to Greenwich time (GMT) in hours Example: +0200

Code: Select all

date("G:i O");

//Or for Thu, 21 Dec 2000 16:01:07 +0200
date("r");

Posted: Tue Mar 22, 2005 3:07 am
by Pyrite
Thanks, date("0"); was what i was looking for, thank you!

dunno how i missed that.

Posted: Tue Mar 22, 2005 4:36 am
by Pyrite
Is there perhaps a php function or rather to produce the date and time given the value of the GMT difference that date("O") produces (not necessarily for the server php is running on though). ??

Posted: Tue Mar 22, 2005 4:44 am
by n00b Saibot
you can put strtotime to good use here.;)

Posted: Tue Mar 22, 2005 4:53 am
by Pyrite
How so? :roll:

Posted: Tue Mar 22, 2005 5:15 am
by n00b Saibot

Code: Select all

$dt = date("O");
echo $dt."<br>"; //output the offset from GMT
echo date("Y m d h:m:s",strtotime("- ".substr($dt,1,3)." hours ".substr($dt,3)." minutes ")); //output the GMT time
The above code calculates GMT Time just by using the difference returned by date("O").This is just an example which you could have written yourself. But sorry to say, you didn't have time for this while you were rolling your eyes. :x


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Mar 22, 2005 5:20 am
by Pyrite
Sir, I wasn't rolling my eyes, i chose that icon to represent how clueless I am to what you are talking about. I appreciate your help for me, thank you! Now let me go try this out. :D

Posted: Tue Mar 22, 2005 5:24 am
by n00b Saibot
Aw! Sorry for that! I just overcame myself :( Try that and tell me if any help is wanted :)

Posted: Tue Mar 22, 2005 5:28 am
by Pyrite
No, doesn't do what I'm wanting, heck, not really sure what it does. That code gives me:

Code: Select all

+0700
2005 03 25 04:03:45
Except, the day right now is March 22 and the time is 18:28 PM

Posted: Tue Mar 22, 2005 5:31 am
by Pyrite
Let me explain what I'm trying to come up with, I am trying to make a function that will return the date and time given an GMT offset (a number between -12 to +13, eg. +7 = Thailand or -05 = EST).

Posted: Tue Mar 22, 2005 6:31 am
by n00b Saibot
oops my bad! used m instead of i. :lol:
sorry it took so long. i was busy with my office work. ;)
here's you function. it takes the string returned by date("O") and returns a UNIX timestamp for the given locale difference.

Code: Select all

function returnLocalTime($GMTDiff)
{
$sign = substr($GMTDiff,0,1);
$hr = substr($GMTDiff,1,2);
$min = substr($GMTDiff,3);
switch($sign)
{
case '+':
return mktime(gmdate('H')+$hr,gmdate('i')+$min,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y"));
break;
case '-':
return mktime(gmdate('H')-$hr,gmdate('i')-$min,gmdate("s"),gmdate("m"),gmdate("d"),gmdate("Y"));
break;
}
}
Usage:

Code: Select all

echo date("d-M-Y H:i:s",returnLocalTime("+0530"));
//printed 22-March-2005 18:12:01 on my comp

feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Mar 22, 2005 6:42 am
by Pyrite
Sorry, that doesn't do it. I've got the code down now myself, may be you can make some suggestions to clean it up.

Code: Select all

<?php

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

	$tz = substr(date("O"),0,3);
	if (substr($tz, 0, 1) == "+") {
   		$tz = substr($tz,1,2);
    }

echo "Local GMT is ".$tz."<p>";

$target_gmt = "-07";

echo "Target GMT is ".$target_gmt."<p>";

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

echo "<p>The Time Difference Between $tz and $target_gmt is $time_diff<p>";

$now = time();
$stamp  = ($now + ($time_diff*60)*60);

echo "The Date/Time of Target is ".date("F j, Y, g:i a", $stamp);


?>
As you can see, $target_gmt is set to "-07" which is USA Mountain Time. So the output for me is (should also be the same for you).

Code: Select all

Local GMT is 07

Target GMT is -07

The Time Difference Between 07 and -07 is -14

The Date/Time of Target is March 22, 2005, 5:43 am

Posted: Tue Mar 22, 2005 7:00 am
by n00b Saibot
Pyrite wrote:Sorry, that doesn't do it.
How so. :o

Posted: Tue Mar 22, 2005 7:06 am
by Pyrite
Well mine isn't fullproof either, but I did this with yours:

echo date("d-M-Y H:i:s",returnLocalTime("-0700"));

And it gave me:

21-Mar-2005 23:05:44

Which is not correct, cause it is 22-Mar-2005 06:06 in GMT-7