如果我有以下代码,我会从我的数据库中输出项目。我想知道如何检查某些日期是否与今天相同,然后输出的数据将回显“今天”而不是 7-23-15,如果我正在查看发布的内容,“昨天”也是如此昨天。
然后我想看看是否可以断开数据库中日期时间条目的时间部分,只回显时间?
我的数据库中有 DATETIME 作为我的数据类型。
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
//$result2 = $stmt2->get_result();
if (!$stmt2) {
throw new Exception($con->error);
}
}
$num_rows2 = $stmt2->num_rows;
if($num_rows2) {
while($stmt2->fetch()) {
$post_id;
$post_category_id;
$post_topic_id;
$post_creator;
$post_content;
$post_date;
最佳答案
替换:
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
与:
$stmt2 = $con->prepare("SELECT `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, IF(DATE(`post_date`) = CURDATE(), 'Today', DATE(`post_date`)) FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
这将只返回日期部分。如果是当前日期,它将返回“今天”。
关于php - 将日期时间输出更改为今天并中断时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595089/