如何分别回显日期部分
我在网上找到了他们回答不同问题的地方,但我仍然不知道如何将其应用到我的代码中。
基本上我希望在一个回显行中显示月份,在另一回显行中显示日数。
我的代码中有办法做到这一点吗?
<?php
// Connects to your Database
$data = mysql_query("SELECT id, title, description, location, date FROM Calendar")
or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
echo "<article>";
echo "<date>";
echo "<h3>" .$row['date(month)'] . "</h3>";
echo "<h4>" .$row['date(day)'] . "</h4>";
echo "</date>";
echo "</article>";
}
?>
I have found place online where they answer different things, but I still don't get how I can apply it to my code.
Basically I want the month in one echo-line, and the day-number in another echo.
Is there away to do that in my code?
<?php
// Connects to your Database
$data = mysql_query("SELECT id, title, description, location, date FROM Calendar")
or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
echo "<article>";
echo "<date>";
echo "<h3>" .$row['date(month)'] . "</h3>";
echo "<h4>" .$row['date(day)'] . "</h4>";
echo "</date>";
echo "</article>";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该能够直接通过 strtotime 传递日期:
但这取决于日期存储在数据库中的格式。这应该适用于
TIMESTAMP
类型列或 UNIX 时间戳,或(大多数情况下)日期的字符串表示形式。You should be able to just pass the date straight through strtotime:
But this will depend on the format the date is stored in the database. This should work for a
TIMESTAMP
type column or a UNIX timestamp, or (for the most part) a string representation of a date.这是一种在 MySQL 查询中提前获取它们的方法,作为稍后在 PHP 中解析它们的替代方法。
然后访问它们:
或者要获取月份的名称而不是数字,请使用 DATE_FORMAT(date, '%M') 作为月份
MySQL
DATE_FORMAT()
文档Here's a way to get them ahead of time in your MySQL query, as an alternative to parsing them out later in PHP.
Then to access them:
Alternatively to get the month's name instead of number, use
DATE_FORMAT(date, '%M') as month
MySQL
DATE_FORMAT()
documentation首先获取日期的 Unix 时间,然后格式化两个不同的字符串。
You first get the unix time of the date and then format two different strings.
如果您想将日期精确地显示为月份名称,可以使用“F”。
像这样:
$month = date('F', strtotime($row['dateRepaired']));
If you want to echo date as exactly the name of the month, you can use 'F'.
Like this:
$month = date('F', strtotime($row['dateRepaired']));