PHP 中的日期格式 - 找不到解决方案



我对PHP非常陌生,我已经研究了一段时间,并在这里尝试了一些没有奏效的解决方案。

我一直在为大学的一个班级制作一个网站,一个节日网站,我一直试图显示一个特定的用户输入日期(通过CMS系统(,而不是硬编码日期(这似乎是我在网上能找到的所有解决方案(。

下面是我的代码示例:

$query = "SELECT * FROM gatherings";
$select_all_events_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_all_events_query)) {
$gather_id = $row['gather_id'];
$gather_name = $row['gather_name'];
$gather_date = $row['gather_date'];
$gather_img = $row['gather_img'];
$gather_content = substr($row['gather_content'], 0, 150);
$gather_supporting = $row['gather_supporting'];
$gather_tags = $row['gather_tags'];
$gather_status = $row['gather_status'];
if($gather_status == 'Published') {
?>
<div class="col-sm-6">
<div class="eventcard-left card">
<div class="card-body card-body-align text-center">
<h1 class="card-title"><?php echo $gather_name; ?></h1>
<p class="card-text"><?php echo $gather_content; ?></p>
<h1 class="card-title"><?php echo date_format($gather_date,"d/m/Y"); echo "<br>"?></h1>
<a class="btn btn-outline" href="event.php?p_id=<?php echo $gather_id; ?>" class="btn btn-primary">View Event</a>
</div>
</div>
</div>
}

无论我做什么,它总是显示为YYYY-MM-DD,我知道这可能是一个非常简单的解决方案,但我已经尝试了很多,无法更改。当我创建我的活动时,我也尝试过改变这一点,但运气不好。如果你还有什么需要帮助我的,我可以提供。

if(isset($_POST['create_event'])) {
$gather_name = $_POST['gather_name'];
$gather_date = date('d-m-Y');
}

此外,关于日期和时间,我试图将时间显示为00:00,但只返回00:00:00,希望这两种解决方案相似。

您需要首先创建一个DateTime对象。尝试:

echo date_create($gather_date)->format("d/m/Y");

这是一个简短的表示法,用于创建DataTime对象,然后直接对其调用format()方法。您将日期字符串作为参数传递给DateTime的构造函数,或者在本例中传递给date_create()函数。

一个较长的符号看起来像:

echo (new DateTime($gather_date))->format("d/m/Y");

最新更新