• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

PHP日期时间错误| date_create()[function.date-create]:依靠系统的时区设置是不安全的

php 来源:Master Chef 6次浏览

我在将日期发布到我的数据库时遇到问题,当我重新加载页面时检索它。我希望它自动为最后一项输入时间戳。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

===========解决方案如下:

您发送的是类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)