spoofing time on server
I have a web page that receives currently playing songs on my local radio stations. It gets song data from Radiotime.com. My problem is that the server that hosts my page is in the American Central time zone, and as such, the times in the data received by the server are in Central time. The radio stations and I, however, are in the Eastern time zone, so I would prefer the times shown on my web page to be in Easter开发者_C百科n time. Can I spoof the time zone shown by the server, or can I convert the times shown on the page somehow? Any help will be greatly appreciated. -Austin
If you're using PHP 5.1 or greater you can use date_default_timezone_set
:
date_default_timezone_set('America/New_York'); // set timezone to Eastern
Use a supported timezone string with the date_default_timezone_set()
function.
You probably want something like:
date_default_timezone_set('America/New_York');
If you want the time to be different for the entire server, you could change the server's timezone. On Debian-derived distributions such as Ubuntu, you could run sudo dpkg-reconfigure tzdata
to configure the timezone.
If you just want the webserver to run in a different timezone, you could set the TZ
environment variable in the /etc/init.d/nginx
or similar init script.
You can probably also set the TZ
environment variable inside your specific program; most languages provide a setenv
or similar function, that would affect the output of many time-related functions. (gmtime(3)
of course won't care.)
try
date_default_timezone_set('America/New_York');
$script_tz = date_default_timezone_get();
or try with
putenv("TZ=US/Eastern");
reference
精彩评论