我有一个查询返回一个日期,我想在我的页面显示给用户之前格式化它。但是,当我使用格式时,日期显示为空(显示1969年)。这里是获取数据的代码:从mysqli返回的PHP格式日期
$sql = 'SELECT u.username, td.postingText, td.createdOn, td.lastUpdated
FROM threadDetails td, users u
WHERE blogThreadId = ?
AND td.userId = u.userId
order by td.createdOn DESC';
$stmt = $conn->stmt_init();
$stmt->prepare($sql);
$stmt->bind_param('s', $blogThreadId);
$stmt->bind_result($username, $postingText, $createdOn, $lastUpdated);
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
while ($stmt->fetch())
{
$rowofdata=array(
'username' => $username,
'postingText' => $postingText,
'createdOn' => $createdOn,
'lastUpdated' => $lastUpdated,
);
$results[] = $rowofdata;
}
$stmt->close();
当我把它打印出来用下面的代码(在不同的功能):
foreach ($threadData as $threadDetailItem)
{
$msg = sprintf ("<tr>%s %s %s %s %s </tr>\n",
"<td>" . $threadDetailItem['threadTitle'] . "</td>",
"<td>" . $threadDetailItem['username'] . "</td>",
"<td>" . $threadDetailItem['createdOn'] . "</td>",
"<td>" . $threadDetailItem['threadCount'] . "</td>",
"<td>" . $threadDetailItem['lastUpdated'] . "</td>");
echo $msg;
}
它打印出来(我创建了或上次更新字段):
2012-04-15 16点14分55秒
当我取代的sprintf线部分:
"<td>" . $threadDetailItem['createdOn'] . "</td>",
有:
"<td>" . date("F j, Y, g:i a", $threadDetailItem['createdOn']) . "</td>",
我得到
“1969年12月31日,7:33 PM” 在我phplog陈述遇到非合式数值的消息。我需要做什么才能正确显示正确的日期?我尝试将sprintf从%s更改为%d,但这不起作用。
在此先感谢您的建议。
直接从MySQL中选择格式化的日期:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format任何东西都不会浪费CPU时间做从string-> native-> string->等等的往返转换... – 2012-04-16 15:21:36