Sisir
Sisir

Reputation: 2804

time() function

time() returns the current time measured in the number of seconds since the

Unix Epoch (January 1 1970 00:00:00 GMT).

My question is:

  1. Does it related to server time? Does output of time(); from a server locating on UK is different from a server located on USA? Or the unix time is calculated on GMT?
  2. I am calculating local time and i have gmt offsets. Do i have to add GMT offset from the unix time to find out the current local time.

Upvotes: 2

Views: 192

Answers (2)

Álvaro González
Álvaro González

Reputation: 146593

  1. The Unix epoch is a fixed moment in time, not a local time, so the number of seconds since them is the same no matter where you are. Server time only affects if the server's clock is wrong :)

  2. You shouldn't care about GMT offsets if the function you use to display dates is timezone aware and properly configured:

    <?php
    
    $now = time();
    
    date_default_timezone_set('Europe/Madrid');
    echo date('Y-m-d H:i:s O e', $now) . PHP_EOL;
    
    date_default_timezone_set('America/Hermosillo');
    echo date('Y-m-d H:i:s O e', $now) . PHP_EOL;
    

    ... prints:

    2011-08-09 13:33:58 +0200 Europe/Madrid
    2011-08-09 04:33:58 -0700 America/Hermosillo
    

Upvotes: 2

Nahydrin
Nahydrin

Reputation: 13517

As outlined at the start of the manual and in your question, the time returned is seconds since the Unix Epoch in GMT.

It is not your local time, you need to modify timezone settings or modify the value returned from time() to be your local time.

Upvotes: 1

Related Questions