如何分别回显日期部分

发布于 2024-12-06 23:11:02 字数 574 浏览 1 评论 0原文

我在网上找到了他们回答不同问题的地方,但我仍然不知道如何将其应用到我的代码中。

基本上我希望在一个回显行中显示月份,在另一回显行中显示日数。

我的代码中有办法做到这一点吗?

  <?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔戏命师 2024-12-13 23:11:02

您应该能够直接通过 strtotime 传递日期:

$month = date('m', strtotime($row['date']));
$day = date('d', strtotime($row['date']));

但这取决于日期存储在数据库中的格式。这应该适用于 TIMESTAMP 类型列或 UNIX 时间戳,或(大多数情况下)日期的字符串表示形式。

You should be able to just pass the date straight through strtotime:

$month = date('m', strtotime($row['date']));
$day = date('d', strtotime($row['date']));

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.

一个人的夜不怕黑 2024-12-13 23:11:02

这是一种在 MySQL 查询中提前获取它们的方法,作为稍后在 PHP 中解析它们的替代方法。

$data = mysql_query("SELECT id, title, description, location, DATE_FORMAT(date, '%m') AS `month`, DATE_FORMAT(date, '%d') AS `day` FROM Calendar") 
  or die(mysql_error());

然后访问它们:

while($row = mysql_fetch_array( $data )) 
{ 
   echo $row['month'];
   echo $row['day'];
}

或者要获取月份的名称而不是数字,请使用 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.

$data = mysql_query("SELECT id, title, description, location, DATE_FORMAT(date, '%m') AS `month`, DATE_FORMAT(date, '%d') AS `day` FROM Calendar") 
  or die(mysql_error());

Then to access them:

while($row = mysql_fetch_array( $data )) 
{ 
   echo $row['month'];
   echo $row['day'];
}

Alternatively to get the month's name instead of number, use DATE_FORMAT(date, '%M') as month

MySQL DATE_FORMAT() documentation

2024-12-13 23:11:02

首先获取日期的 Unix 时间,然后格式化两个不同的字符串。

You first get the unix time of the date and then format two different strings.

听不够的曲调 2024-12-13 23:11:02

如果您想将日期精确地显示为月份名称,可以使用“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']));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文