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

get GMT in string format

Post by Pyrite »

There a way to get the server's time in GMT string format.

ie. GMT+/-#, eg. GMT-7 or GMT+13
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post by Pyrite »

Thanks, date("0"); was what i was looking for, thank you!

dunno how i missed that.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post 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). ??
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 »

you can put strtotime to good use here.;)
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

How so? :roll:
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 »

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

Post 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
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 »

Aw! Sorry for that! I just overcame myself :( Try that and tell me if any help is wanted :)
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

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

Post 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).
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 »

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

Post 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
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 »

Pyrite wrote:Sorry, that doesn't do it.
How so. :o
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 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
Post Reply