faqts : Computers : Programming : Languages : PHP : Function Libraries : Date and Time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 14 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

how do i display different time zone setting using a function?

Apr 19th, 2003 22:48
Shashank Tripathi, Hemant Kumar, sterling hughes


Use the following function give by Sterling Hughes in his book PHP 
Developer's Cookbook. Must read by every serious PHP developer.
// Date Function
function universaltime($offset1,$offset2){
	global $weekday;
	$day = gmdate("d");
	$month = gmdate("m");
	$hour = gmdate("g");
	$year = gmdate("Y");
	$minutes = gmdate("i") + $offset2;
	$seconds = gmdate("s");
	$hour = gmdate("H") + $offset1;
	$tm_ar = getdate(mktime
($hour,$minutes,$seconds,$month,$day,$year,$weekday));
	return ($tm_ar);
}
$offset1="5";
$offset2="30";
$currdate = universaltime($offset1,$offset2);
$pdate="<DIV align=right><FONT face=\"Verdana, Arial, Helvetica, sans-
serif\" size=1>".$currdate["weekday"].", ".$currdate
["month"]." ".$currdate["mday"].", ".$currdate["year"].". (".$currdate
["hours"].":".$currdate["minutes"].":". $currdate["seconds"].")
</font></div>";
print $pdate;
--------------------------
EDIT by SHASHANK TRIPATHI:
April 2003
Actually, the above code may have been good for a book sample, but
calling gmdate() functions, and composing them back into a timestamp
with mktime() is just not the most efficient way of doing it. The
following code achieves the same result, in much fewer lines. 
Please also note that in the above sample (and this was not explained
clearly) the "offset1" is the hours difference and the "offset2" the
minutes difference. In the sample below, these values are "oH" and "oM"
respectively as commented.  
<?
$oH = "+09";  // Hours difference. Nice to indicate the sign
$oM = "00";   // Minutes difference
// Current GMT time
echo 'Local Date: '  .
      gmstrftime("%a, %x %X %p [GMT]", time()).'<br/>';
// Offset time
echo 'Offset Date: ' .
      gmstrftime("%a, %x %X %p [$oH$oM]", time() + ($oH*3600 + $oM*60));
?>
-----
NOTES
o The date format within the gmstrftime can be modified to your
intentions, this is just a sample.
o Explanation of what is being done: since the time() in php contains a
unix timestamp (i.e., seconds) we just add the offset hours multiplied
by 3600 (which is the number of seconds in 1 hour) and the offset hours
multiplied by 60 (which is the number of seconds in 1 minute).