这种关系与Access DateTime->date in php有关,但实际上是另一回事。我有一些从SQL服务器获取DateTime对象的php。我转换它使用
echo $mydate->format('Y-m-d H:i:s');
所以它只是一个字符串或至少我认为它。 (我甚至使用显式类型转换来确保)。
这是代码,
} else {
echo "<br/>object ";
$dt = (string) $row[$column]->format('Y-m-d H:i:s');
$this->retVal["nonEmpty"] = true;
$this->retVal["value"] = (string) $dt;
echo $this->retVal["value"]; //Returns a string, no issue.
}
当“字符串”从类中传回时,会出现问题。
$convert = new DataConv;
$convert->conv($newConnectionThree, $fieldNames[$cnt1][$cnt2]["Type"],$fieldNames[$cnt1][$cnt2]["Size"], $tableNames[$cnt1], $columnNames[$cnt1][$cnt2]);
$fieldNames[$cnt1][$cnt2]["Type"] = $convert->type;
$returned = $convert->retVal;
echo $returned["value"]; //Returns error "Catchable fatal error: Object of class DateTime could not be converted to string".
var_dump($returned["value"]); //Shows Datetime object.
现在又回到了Datetime对象。我认为没有任何理由将其转换回来。我只是想知道是否有人得到答案至少为什么会发生这种情况,但解决方案会有所帮助。在课外进行转换的任何东西都没有。
答案 0 :(得分:0)
我发现了问题所在。这与我的想法无关。 无论如何,这是解决方案。
private function get($conn, $table, $column){
$params = array();
$expl = array();
$wrkStrg;
$tsql = "SELECT [" . $column . "] FROM " . $table;
$options = array("scrollable" => SQLSRV_CURSOR_KEYSET);
$getTableData = SQLSRV_QUERY($conn, $tsql, $params, $options);
if ($getTableData){
$row = SQLSRV_FETCH_ARRAY($getTableData, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_FIRST);
if ($row !== NULL){
if (!is_object($row[$column])){
if (!preg_match('/[\"\'\&\<\>]/', $row[$column])){
if (($this->type === "Image") or ($this->type === "Var Binary")){
$this->retVal["nonEmpty"] = true;
$this->retVal["value"] = bin2hex($row[$column]);
} else {
$this->retVal["nonEmpty"] = true;
$this->retVal["value"] = $row[$column]; //These two lines.
}
} else {
$expl = explode("\"", $row[$column]);
$wrkStrg = implode(""", $expl);
$expl = explode("'", $wrkStrg);
$wrkStrg = implode("'", $expl);
$expl = explode("&", $wrkStrg);
$wrkStrg = implode("&", $expl);
$expl = explode("<", $wrkStrg);
$wrkStrg = implode("<", $expl);
$expl = explode(">", $wrkStrg);
$wrkStrg = implode(">", $expl);
$this->retVal["nonEmpty"] = true;
$this->retVal["value"] = $wrkStrg;
$expl = array();
$wrkStrg = Null;
}
} else {
$this->retVal["nonEmpty"] = true;
$this->retVal["value"] = $row[$column]->format('Y-m-d H:i:s');
}
//Used to be here! :(
}
} else {
echo "<p>Table: " . $table . "<br/>Column: " . $column . "</p>";
echo "<p>Falure: Seven</p>";
die(var_dump(SQLSRV_ERRORS(), true));
}
}
所以我只需要在那里添加其他内容并移动这两行。我很尴尬,我没有早点看到。
答案 1 :(得分:-1)
you can serialized an object to string and unserialize string to object
read this reference
http://php.net/manual/en/language.oop5.serialization.php