note 93154 deleted from function.date by tularis
| From: | tularis@php.net | Date: | Thu, 09 Feb 2012 20:56:00 +0000 |
| Subject: | note 93154 deleted from function.date by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185982@lists.php.net to get a copy of this message | ||
Note Submitter: kenaniah at gmail dot com
----
This function behaves like date, but allows you to output the date in a given time zone locale.
<?php
function date_at_timezone($format, $locale, $timestamp=null){
if(is_null($timestamp)) $timestamp = time();
//Prepare to calculate the time zone offset
$current = time();
//Switch to new time zone locale
$tz = date_default_timezone_get();
date_default_timezone_set($locale);
//Calculate the offset
$offset = time() - $current;
//Get the date in the new locale
$output = date($format, $timestamp - $offset);
//Restore the previous time zone
date_default_timezone_set($tz);
return $output;
}
//Examples
$t = time();
print date("g:i A T", $t); //4:16 PM PDT
print date_at_timezone("g:i A T", "America/New_York", $t); //7:16 PM EDT
print date_at_timezone("g:i A T", "Pacific/Samoa", $t); //12:16 PM SST
print date("g:i A T", $t); //4:16 PM PDT
?>