2017-12-03 163 views
0

我在将日期发布到我的数据库时遇到问题,当我重新加载页面时检索它。我希望它自动为最后一项输入时间戳。PHP日期时间错误| date_create()[function.date-create]:依靠系统的时区设置是不安全的

<!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Location Tracker</title> 
    </head> 
    <body> 

    <hl> ACW Location Tracker </hl> 

<?php 
    $server = 'SQL2008.net.dcs.hull.ac.uk'; 

    $connectionInfo = array("Database"=>"rde_531545"); 
    $conn = sqlsrv_connect($server,$connectionInfo); 
    $query='create table Location '; 
    $query .= '(Username int NOT NULL IDENTITY(500, 23), First_Name varchar(50) NOT NULL, Surname varchar(50) NOT NULL, Current_Location varchar(50) NOT NULL, Date datetime NOT NULL, PRIMARY KEY (Username))'; 
    $result = sqlsrv_query($conn, $query); 

    if (!$result) 
    { 
     if(($errors = sqlsrv_errors()) != null) 
     { 
     foreach($errors as $error) { 
      echo "<p>Error: ".$error[ 'message']."</p>"; 
     } 
     } 
    } 
    else { 
     echo "<p>DB successfully created</p>"; 
    } 
    sqlsrv_close($conn); 

    $connectionInfo = array("Database"=>"rde_531545"); 
    $conn = sqlsrv_connect($server,$connectionInfo); 
    $insert_query = "INSERT INTO Location (First_Name, Surname, Current_Location, Date) VALUES (?, ?, ?,?)"; 
    $params = array("John","Doe","Hull", Date); 
    $result = sqlsrv_query($conn,$insert_query,$params); 
    $params = array("Jane","Doe","Hull", Date); 
    $result = sqlsrv_query($conn,$insert_query,$params); 

    $LocationQuery='SELECT Username, First_Name, Surname, Current_Location, Date FROM Location'; 
    $results = sqlsrv_query($conn, $LocationQuery); 
    if ($results) while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) 
    { 
     echo '<p>'.$row['Username'].' '.$row['First_Name'].' '.$row['Surname'].' '.$row['Current_Location'].' '.$row['Date'].'</p>'; 
    } 
?> 
    </body> 
</html> 

我得到的错误是:

Notice: Use of undefined constant Date - assumed 'Date' in C:\RDEUsers\NET\531545\Location.php on line 44

Warning: date_create() [function.date-create]: It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of enter code here those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for '0.0/no DST enter code here ' instead in C:\RDEUsers\NET\531545\Location.php on line 49

Catchable fatal error: Object of class DateTime could not be converted to string in C:\RDEUsers\NET\531545\Location.php on line 51

回答

1

您发送的是类Date,而不是发送一个字符串 '2017年2月3日'。

的错误是在$params = array(....., Date);

第二排从该片的代码:

$insert_query = "INSERT INTO Location (First_Name, Surname, Current_Location, Date) VALUES (?, ?, ?,?)"; 
$params = array("John","Doe","Hull", Date); 

你需要创建一个Date对象$date = new DateTime(); 进入查询,你需要提取字符串从中,与format功能:

$dateStr = $date->format(''Y-m-d H:i:s''); 

然后,使用$ dateStr在PARAMS:$params = array("John","Doe","Hull", $dateStr);


编辑1:

$date = new DateTime(); 
$dateStr = $date->format('Y-m-d H:i:s'); 
$params = array("John","Doe","Hull", $dateStr); 
+0

您好我已经走了大约尝试这样做,但是我还是不太明白你的意思,我只是刚刚开始学习这一点。我必须把这个放在哪里:$ date = new DateTime();和$ dateStr = $ date-> format(''Y-m-d H:i:s'');. –

+0

不要把'Date'放在'$ params'中。 您应该首先创建一个'dateTime'对象,然后使用'format'方法来获取日期字符串。 请看我在编辑1下的答案中添加的代码: – backbone

相关问题