0% found this document useful (0 votes)
280 views2 pages

Convert The Time Zone in PHP

The document discusses displaying time zones in PHP. It provides a PHP function, date_at_timezone, that allows displaying the date and time in a specified timezone by passing the timezone and date format as arguments. The function switches the default timezone, calculates the offset, gets the date in the new timezone, then restores the original timezone. Examples are given showing how to display the time in different timezones like New York, Los Angeles, and Samoa.

Uploaded by

TechnoTiger
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
280 views2 pages

Convert The Time Zone in PHP

The document discusses displaying time zones in PHP. It provides a PHP function, date_at_timezone, that allows displaying the date and time in a specified timezone by passing the timezone and date format as arguments. The function switches the default timezone, calculates the offset, gets the date in the new timezone, then restores the original timezone. Examples are given showing how to display the time in different timezones like New York, Los Angeles, and Samoa.

Uploaded by

TechnoTiger
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

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….

If you like this post please visit http://technotiger87.blogspot.com

You might also like