I have a website in which all records are inserted according to server datetime which is around 6 hours behind GMT. I want to display those datetimes in GMT without changing insertion datetime. I mean is there any global setting in PHP which wil开发者_如何学JAVAl allow me to display datetime in GMT without affecting every line where I am displaying datetime? And I also don't want to change the insert script which will continue to insert datetime according to what ever current timezone is.
Thanks
Unfortunately, there is only one global timezone setting for PHP:
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/Los_Angeles
It affects both parsing and rendering, so changing this value will affect all of your dates.
If you just care about display, you can use the gmdate()
function to render GMT dates. It works exactly like the date()
function, except it renders the dates in GMT instead. See the gmdate documentation for more details.
You will need to modify the timezone of any datetimes you pull out that are server time, you cannot modify the default timezone (completely anyway, see the procedural approach that changes it and sets it back again). There are 3 options described below.
I'd recommend using the DateTIme object as it offers the greatest simplicity and flexibility.
Using The DateTime Object
You shouldn't do anything when you create the DateTime, so it creates it with the server timezone, but then you need to call setTimezone.
Here's an example:
// Could just call this once, and use a reference to it.
$timezone = new DateTimeZone('GMT');
$insertDate = new DateTime($row['insertDate']);
$insertDate->setTimezone($timezone);
Now $insertDate
would be in the GMT timezone converted from whatever your server's timezone is.
Using Procedural Date/Time Functions
If you want to use the procedural functions, in your case you could use gmdate or if you needed another timezone other than GMT you would have to continuously change the default timezone using date_default_timezone_set
// Storing so you can restore the current default
$serverTimezone = date_default_timezone_get();
/**
* Get the unix timestamp for the insertDate
* This converts from the default timezone to UTC
*/
$insertDate = strtotime($row['insertDate']);
date_default_timezone_set('GMT');
/**
* This displays the date in the new default timezone GMT, based
* on the UTC timezone of the unix timestamp
*/
echo date('Y-m-d H:i:s', $insertDate);
// Reset the timezone
date_default_timezone_set($serverTimezone);
This could be abstracted into its own function to save you the hassle.
Using gmdate - Only works for GMT
Similar concept as the procedural example shown above, except we don't need to play with the default timezone.
/**
* Get the unix timestamp for the insertDate
* This converts from the default timezone to UTC
*/
$insertDate = strtotime($row['insertDate']);
/**
* This displays the date in GMT, based
* on the UTC timezone of the unix timestamp
*/
echo gmdate('Y-m-d H:i:s', $insertDate);
Use date.timezone
In your php.ini, add the following line:
date.timezone = "Etc/GMT-6"
To see all the potential formats, check out this
精彩评论