Grab the RSS feed

Convert the time zone in PHP

In my application the client want to display the timezone in PST.
But default the time is displayed in MST.That is we get the time from server and displayed in aparticular page.
But the client want to show only in the particular page in PST.So I get the Program from PHP http://www.php.net/manual/en/function.date.php and use it.

Here I need to change the America/New_York to America/Los_Angels and it display the PST time

If you want it to display in different format change the argument in $format.Instead of g:i A T use D,h A T etc.
#

< ?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
? > 

Leave the comments to improve us….
Click Here to download as word document...

0 comments:

  •  
    Real Time Web Analytics