user4951
user4951

Reputation: 33090

If value of time() in PhP not influenced by timezone then why date function default to it?

According to http://www.php.net/manual/en/function.date.php

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

However, in definition of time() there is no mention that it's time zone dependent. Which one is right?

Upvotes: 1

Views: 1802

Answers (4)

user4951
user4951

Reputation: 33090

I think I know the issue.

time() it self is time zone independent.

However,

date() is time zone dependent. How the data is formatted depend on date_default_timezone_set

So, following Asad's answer

date_default_timezone_set("UTC");
echo "UTC:".date(...);
echo "<br>";

date_default_timezone_set("Europe/Helsinki");
echo "Europe/Helsinki:".date(...);
echo "<br>"; 

will produce different value. They both use time

Upvotes: 0

user1726343
user1726343

Reputation:

No, the value returned by time() is timezone independent:

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

date_default_timezone_set("Europe/Helsinki");
echo "Europe/Helsinki:".time();
echo "<br>"; 

Both output the same value.

Regarding your edit, the return value of time() depends on what the current time on your machine is. The current time on your machine is typically set by specifying a time zone, as well as a date + time.

When we say the value returned by time() is timezone independent, we mean that at any given instant, the correct value for UTC time at all locations on earth is the same.

Suppose a person in Japan were to correctly set their system time (along with timezone), and another person in India were to do the same. At any given instant, if they were to invoke time() simultaneously, they would get the same value.

Upvotes: 4

deceze
deceze

Reputation: 522125

I think the documentation is just slightly vague, meaning "local" as in "of the machine it's running on". Or you could make it to mean that since date formats the timestamp according to the set timezone, the value returned by date will be the "local" time.

I.e., the "local" doesn't really mean anything here.

Upvotes: 2

paulgrav
paulgrav

Reputation: 624

time() returns the number of seconds since 00:00 1/1/1970 GMT.

The elapsed number of seconds since the UNIX epoch is the same no matter in which timezone you are.

Upvotes: 6

Related Questions