Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

date - Setting timezone to UTC (0) in PHP

Why does this work?

date_default_timezone_set('Australia/Currie');

But this doesn't seem to take any effect at all?

date_default_timezone_set('UTC');

This value doesn't change when setting the timezone to UTC:

echo date('Y-m-d H:i:s', time());

I'm using php 5.2.13, and the timezone of my server is:

$server_tz = date_default_timezone_get();
echo $server_tz; //outputs 'America/Guayaquil'

This is the original code:

echo time() . "<br>
";
date_default_timezone_set('UTC');
echo time() . "<br>
";

Output:

1317235130
1317235130
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is that you're displaying time(), which is a UNIX timestamp based on GMT/UTC. That’s why it doesn’t change. date() on the other hand, formats the time based on that timestamp.

A timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

echo date('Y-m-d H:i:s T', time()) . "<br>
";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time()) . "<br>
";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...