Zend - The PHP Company




Conversion

Add Code


User's Real Time  

Type: code fragment
Added by: j_freeman
Entered: 20/10/2006
Last modified: 02/11/2005
Rating: - (fewer than 3 votes)
Views: 2831
This is just like PHP's date() function, except it observes the user's time settings. Very useful if you want to have times customized for your users, depending on what timezone they are in and whether they are observing DST/Summer Time.


<?php
/*
 $format and $timestamp
   correspond with that of date() and gmdate().
 $timeoffset
   is the time zone offset in seconds (-5:00 GMT would be -18000).
 $dstoffset
   is the Daylight Saving Time/Summer Time (when observed) offset in seconds (one hour would be 3600).
 $dst
   is an indicator of whether Daylight Saving Time/Summer Time is being observed.
*/
function realdate($format$timestamp$timeoffset$dstoffset$dst FALSE)
{
    
$timestamp $timestamp $timeoffset + ((int)$dst $dstoffset);
    return 
gmdate($format$timestamp);
}
?>


Usage Example


<?php
    
if(isset($_REQUEST['submit']))
    {
        
$iOff = (int)$_REQUEST['offset'];
        
$iDST = (int)$_REQUEST['dstoffset'];
        
$bDST = (bool)$_REQUEST['dst'];
        
$strTime '<p>The time is: ' realdate('g:iA'time(), $iOff$iDST$bDST) . '</p>';
    }

// *************************************************************************** \

/*
 $format and $timestamp
   correspond with that of date() and gmdate().
 $timeoffset
   is the time zone offset in seconds (-5:00 GMT would be -18000).
 $dstoffset
   is the Daylight Saving Time/Summer Time (when observed) offset in seconds (one hour would be 3600).
 $dst
   is an indicator of whether Daylight Saving Time/Summer Time is being observed.
*/
function realdate($format$timestamp$timeoffset$dstoffset$dst FALSE)
{
    
$timestamp $timestamp $timeoffset + ((int)$dst $dstoffset);
    return 
gmdate($format$timestamp);
}

// *************************************************************************** \

// List of the time zones and their corresponding offsets.
$aTimeOffsets =
array(
    -
43200 => '[GMT -12:00] International Date Line West',
    -
39600 => '[GMT -11:00] Midway Islands, Samoa',
    -
36000 => '[GMT -10:00] Hawaii-Aleutian Time',
    -
32400 => '[GMT -09:00] Alaska Time',
    -
28800 => '[GMT -08:00] Pacific Time (US &amp; Canada)',
    -
25200 => '[GMT -07:00] Mountain Time (US &amp; Canada)',
    -
21600 => '[GMT -06:00] Central Time (US &amp; Canada)',
    -
18000 => '[GMT -05:00] Eastern Time (US &amp; Canada)',
    -
14400 => '[GMT -04:00] Atlantic Time (Canada)',
    -
12600 => '[GMT -03:30] Newfoundland Time',
    -
10800 => '[GMT -03:00] Brasilia, Buenos Aires, Greenland',
    -
7200 => '[GMT -02:00] Mid-Atlantic Time',
    -
3600 => '[GMT -01:00] Azores, Cape Verde Island',
    
=> '[GMT +00:00] Western Europe Time',
    
3600 => '[GMT +01:00] Central Europe Time',
    
7200 => '[GMT +02:00] Eastern Europe Time',
    
10800 => '[GMT +03:00] Eastern Africa Time',
    
12600 => '[GMT +03:30] Middle East Time',
    
14400 => '[GMT +04:00] Near East Time',
    
16200 => '[GMT +04:30] Kabul Time',
    
18000 => '[GMT +05:00] Pakistan-Lahore Time',
    
19800 => '[GMT +05:30] India Time',
    
20700 => '[GMT +05:45] Kathmandu Time',
    
21600 => '[GMT +06:00] Bangladesh Time',
    
25200 => '[GMT +07:00] Christmas Island Time',
    
28800 => '[GMT +08:00] China-Taiwan Time',
    
32400 => '[GMT +09:00] Japan Time',
    
34200 => '[GMT +09:30] Australia Central Time',
    
36000 => '[GMT +10:00] Australia Eastern Time',
    
39600 => '[GMT +11:00] Soloman Time',
    
43200 => '[GMT +12:00] New Zealand Time'
);
?>

<?php echo($strTime); ?>

<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
    Time Zone:<br />
    <select name="offset">
<?php
    
foreach($aTimeOffsets as $iOffset => $strOffset)
    {
        
$strSelected = ($iOffset == $iOff) ? ' selected="selected"' '';
        echo(
"tt<option value="{$iOffset}"{$strSelected}>{$strOffset}</option>n");
    }
?>
    </select><br />

    <p>DST Offset in seconds:<br /><input type="text" name="dstoffset" value="<?php echo($iDST); ?>" /></p>
    <p><input type="checkbox" name="dst"<?php if($bDST)echo(' checked="checked"'); ?> /> DST being observed?</p>

    <input type="submit" name="submit" value="Show Me The Time" />
</form>


Rate This Script





Search



This Category All Categories