0

使用 codeigniter 我有一个模型来处理与时间相关的函数。我使用date_default_timezone_set('Europe/London');这样所有的时间函数都基于伦敦时间而不是服务器时间(基于美国)。但是,有时我需要知道服务器时间。我尝试$server_time = time();在时区集之前在构造中创建一个变量,但是当我在函数中引用它时,我只会返回伦敦时间。像这样...

function __construct(){
    parent::__construct();
    // server time
    $this->server_time = time(); 

    // set base time for all php time() functions!
    date_default_timezone_set('Europe/London');
}

public function serverTime(){
   return $this->server_time; // returns London time not server time
}

如何获得实际的、真实的服务器时间?

4

2 回答 2

2

以下将date在 shell 中执行命令:

echo shell_exec( 'date' );

这将为您提供时间,就像您在服务器上的命令行中键入它一样;

于 2013-09-23T13:59:15.620 回答
0

您将要存储两个时区之间的偏移量,而不是 time() 的实际值:

function __construct(){
    parent::__construct();
    $diff_time = time(); 
    date_default_timezone_set('Europe/London');
    $this->diff_time = $diff_time-time(); 
}

public function serverTime(){
   return time()+$this->diff_time;
}
于 2013-09-23T14:01:28.193 回答