2017-12-03 69 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

相關問題