get GMT in string format
Posted: Tue Mar 22, 2005 2:44 am
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
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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");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 timeCode: Select all
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]Code: Select all
+0700
2005 03 25 04:03:45Code: 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 compCode: Select all
andCode: Select all
tags. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]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 amHow so.Pyrite wrote:Sorry, that doesn't do it.