【问题标题】:Display correct format of Data in php在php中显示正确的数据格式
【发布时间】:2019-10-08 14:13:59
【问题描述】:

以下是我在数据库中显示值的代码。我希望输出如下: 2019 年 7 月 21 日,与 2019-07-21 不同。有什么想法和帮助吗?

 <?php $servername = "localhost";
 $username = "root";
 $password = "";
  $dbname = "software";


  $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
  if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
  }



   SELECT * From (select * from training_calendar ORDER BY        training_calendar_id DESC LIMIT 2) AS name ORDER BY training_calendar_id   LIMIT 1
 $sql = "SELECT * From (select * from training_calendar ORDER BY   training_calendar_id DESC LIMIT 2) AS name ORDER BY training_calendar_id  LIMIT 1";
 $result = $conn->query($sql);

 //if ($result->num_rows > 0) {



    while($row = $result->fetch_assoc()) {
      echo " ". $row["date_of_training"]. "<br> ";


  }

  //} else {
     //  echo "0 results";
  //} ?>

【问题讨论】:

    标签: sql database date format


    【解决方案1】:

    您需要格式化日期,下面的代码将完成您的工作-

    <?php
    
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "software";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $sql = "SELECT * From (select * from training_calendar ORDER BY   training_calendar_id DESC LIMIT 2) AS name ORDER BY training_calendar_id  LIMIT 1";
    $result = $conn->query($sql);
    
    while ($row = $result->fetch_assoc()) {
        echo " " . date("j F Y", strtotime($row["date_of_training"])) . "<br> ";
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      要么在获取数据时使用Mysql的DATE_FORMAT函数,要么使用PHP的日期函数来格式化日期。

      回显日期("j F Y",strtotime("2019-07-21"));

      【讨论】:

        猜你喜欢
        • 2022-01-26
        • 2017-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 1970-01-01
        • 2019-06-26
        相关资源
        最近更新 更多