get GMT in string format
Moderator: General Moderators
- Pyrite
- Forum Regular
- Posts: 769
- Joined: Tue Sep 23, 2003 11:07 pm
- Location: The Republic of Texas
- Contact:
get GMT in string format
There a way to get the server's time in GMT string format.
ie. GMT+/-#, eg. GMT-7 or GMT+13
ie. GMT+/-#, eg. GMT-7 or GMT+13
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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");- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
you can put strtotime to good use here.
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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 timefeyd | Please review how to post code using
Code: Select all
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- Pyrite
- Forum Regular
- Posts: 769
- Joined: Tue Sep 23, 2003 11:07 pm
- Location: The Republic of Texas
- Contact:
No, doesn't do what I'm wanting, heck, not really sure what it does. That code gives me:
Except, the day right now is March 22 and the time is 18:28 PM
Code: Select all
+0700
2005 03 25 04:03:45- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
oops my bad! used m instead of i. 
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.
Usage:
feyd | Please review how to post code using
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;
}
}Code: Select all
echo date("d-M-Y H:i:s",returnLocalTime("+0530"));
//printed 22-March-2005 18:12:01 on my compfeyd | Please review how to post code using
Code: Select all
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]- Pyrite
- Forum Regular
- Posts: 769
- Joined: Tue Sep 23, 2003 11:07 pm
- Location: The Republic of Texas
- Contact:
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.
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
<?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);
?>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- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: