【问题标题】:set timezone to the local time of the client将时区设置为客户端的本地时间
【发布时间】:2015-07-10 00:12:27
【问题描述】:

我在托管服务提供商“byethost.com”上加载了我的 php 脚本。我正在尝试将 created_at 中记录的时间设置为客户端的本地时间,但我总是得到 GTM 时间。我怎样才能让它工作?

 <?php
 $data = json_decode ( file_get_contents ( 'php://input', true ) );

$mac = $data->{'mac'};
$latitude = $data->{'latitude'};
$longitude = $data->{'longitude'};
$route =   $data->{'route'};

$timeZone = "Europe/Berlin";
$print = date_default_timezone_set($timeZone);

$con = new mysqli ( "domin.com", "username", "password", "database" );


// check whether route's table exist.
$results = $con->query ( "SHOW TABLES like 'bus' " ) or die ( mysqli_error () );

if (($results->num_rows) == 1) {$sql = "REPLACE INTO bus(mac, route, latitude, longitude, created_at)
          VALUES( ?, ?, ? , ?, ? )";
  $stmt = $con->prepare($sql);

  if(false === $stmt){
    echo "prepare()  failed: ";
  }

  $rc = $stmt->bind_param("sssss",$mac,$route, $latitude,$longitude, $print);
 echo $rc;
  if ( false===$rc ) {
  echo "bind_param() failed: ";
}

  $rc = $stmt->execute();

  if ( false===$rc ) {
 echo "execute failed.";
  }

  $stmt->close();

} else {
  $create =  "CREATE TABLE bus
       (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
        mac VARCHAR(30) NOT NULL UNIQUE,
        route int(11) ,
     latitude FLOAT(10,6) NOT NULL , 
     longitude FLOAT(10,6) NOT NULL,
     created_at TIMESTAMP NOT NULL" ;
   $stmt = $con->prepare($create) or die ( $con->error );
  $stmt->execute();

  $stmt->close();

}

【问题讨论】:

  • 您可能希望将所有时间存储在 GMT 的数据库中,并以用户的时区显示时间。在这个 SO 问题stackoverflow.com/questions/3792066/… 中查看来自@fijiaaron 的答案。
  • @steve:不,我想将数据(用于测试目的)存储在客户端时区的总线表中。

标签: php mysql


【解决方案1】:

也许可以试试这个?

date_default_timezone_set($timeZone);

时区可以是:

"Europe/Oslo"

有关支持的时区的完整列表,请参阅http://php.net/manual/en/timezones.php

【讨论】:

  • 我按照你说的做了,我收到了错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7 这是第 7 行。created_at TIMESTAMP NOT NULL?我更新了我的代码,请看一下。
  • 老实说,我看不出 date_default_timezone_set() 怎么会导致 SQL 错误? O.o 你能不能试着把它放在你的代码之上(第 6 行和第 7 行到第 2 和 3 行),看看它是否仍然显示第 7 行?
猜你喜欢
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多