我有一个返回日期的查询,我想在将其显示给页面上的用户之前对其进行格式化.但是,当我使用格式时,日期显示为空(显示1969年).以下是获取数据的代码:
$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();
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码打印出来时(在不同的功能中):
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;
}
Run Code Online (Sandbox Code Playgroud)
它打印出来(对于我创建的On或last Updated字段):
2012-04-15 16:14:55
当我替换sprintf行的部分时:
"<td>" . $threadDetailItem['createdOn'] . "</td>",
Run Code Online (Sandbox Code Playgroud)
有:
"<td>" . date("F j, Y, g:i a", $threadDetailItem['createdOn']) . "</td>",
Run Code Online (Sandbox Code Playgroud)
我明白了
"1969年12月31日,下午7:33"和我的phplog中的一条消息,说明遇到了一个非格式化的数值.我需要做什么才能正确显示正确的日期?我已经尝试将sprintf从%s更改为%d,但这不起作用.
提前感谢您的建议.
这是因为$threadDetailItem['createdOn']是一个字符串,该date()函数需要一个数字时间戳.使用strtotime()PHP函数首先将字符串转换为时间戳:
date("F j, Y, g:i a", strtotime($threadDetailItem['createdOn']))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3314 次 |
| 最近记录: |