I ran this script:
echo date_default_timezone_get()."\n";
echo "----\n";
date_default_timezone_set('Asia/Kabul');
echo date_default_timezone_get()."\n";
echo "----\n";
echo time()."\n";
$dt = new DateTime;
echo $dt->format('U') . "\n";
echo "----\n";
date_default_timezone_set('UTC');
echo date_default_timezone_get()."\n";
echo "----\n";
echo time()."\n";
$dt = new DateTime;
echo $dt->format('U') . "\n";
and for all the 4 timestamp values, it says: 1325905766.
I read somewhere that new DateTime always sets UTC as timezone, whatever the timezone setting, so I get that, but what about time()?
A few seconds before, on a different server (probably not in Europe (ideone.com)), it returns 4 times: 1325905723.
That’s the same! (A few seconds diff obviously, but the same timezone.)
time() always and only returns UTC??? What’s going on??
Yes, they both use UTC. Specifically, both
time()and theUformat return the number of non-leap seconds elapsed since midnight UTC on January 1, 1970 (the “Unix epoch”). As the epoch is a fixed point whose definition is independent of the user’s timezone, and as the number of seconds since then is the same in all time zones (ignoring relativistic effects and whatnot),time()returns the same value regardless of time zone settings.