简体   繁体   中英

date_default_timezone_set('UTC') not working

This seems to be weird but I already check everything, and still a weird thing happens.

I can't change the timezone of my php scripts .

First things first: what I did was something like this:

<?php
date_default_timezone_set('UTC');
echo '<br>';
echo date('Y-m-d H:i:s');
?>

this seems to be working fine when I tried this on a test http://codepad.org/rpYZ0flA .

My server's timezone is set to UTC+8:00 Taipei, but when I tried the code above it's not really working. It still shows my current date_time in my server's timezone, not following the code above.

And this is the php.ini configuration of my server:

date/time support                    enabled
"Olson" Timezone Database Version   2012.3
Timezone Database                   internal
Default timezone                     Europe/Berlin 

Why this is happening? Is this already a bug? Or mistake on server_setup or I just missed something in my code?

Thank you.

NOTE: My environment is a Windows 7N running in VM using PHP 5.4.4

FIX:

I got the fix by changing manually the php.ini

Try this

<?php 
echo date('Y-m-d H:i:s T', time()) . "\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time()) . "\n";

here you will find the test result http://codepad.org/gc5oYnLW

If you want only the time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) with timezone. Use the code below:

<?php
date_default_timezone_set("UTC"); 
time()+date("Z");

It should work without any problems.

In case of doubt, check it with this code:

<?php 
date_default_timezone_set('America/Virgin');
echo date('Y-m-d H:i:s T') . "\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T') . "\n";

Here is a codepad preview for your convenience.

The return will look like this (with updated date and time off course):

2017-12-11 03:09:58 AST
2017-12-11 07:09:58 UTC

If that fails, double-check your server configuration… starting with your PHP.ini file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM