doufeixi6014 2016-06-02 09:41
浏览 17
已采纳

发布日期格式并从数据库中选择日期格式

I have a textbox that displays the current date in format: d-m-Y

<input type="text" name="date" class="form-control" value="<?php echo date('d-m-Y');?>"/>

When I save this the date will written to my database like: Y-m-d. For example: the current date is: 02-06-2016. My script writes this to the database as: 2002-06-16.

I am using the following code to save the data to my database:

<?php
 session_start(); 

 $correct = true;

 $date = $_POST['date'] ;

 if($correct){
  $db = new PDO('mysql:host=localhost;dbname=db', 'root', '');

  $query = "INSERT INTO table(date) VALUES (?)";
  $stmt = $db->prepare($query);
  $stmt->execute(array($date));

  header('Location: ./index.php');  
 }
 else
 {
  echo "Error";
 }
?>

What do I need to change in my code so it will post it like: 2016-06-02 Y-m-d or 02-06-2016 d-m-Y?

If d-m-Y is not possible, how can show the data that is written like Y-m-d in the database show as format d-m-Y in my php script?

Edit:

For selecting the date I use:

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

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

$sql = "SELECT * FROM table WHERE id='1' ORDER BY id DESC LIMIT 1";

$sql = date("d-m-Y",strtotime($row['date']));

$result = $conn->query($sql);

if ($result->num_rows > 0) {

     // output data of each row
     while($row = $result->fetch_assoc()) {
                      echo "<td>" . $row["id"]. "</td>";
                      echo "<td>" . $row["date"]. "</td>";
     }                   
} else {
                      echo "<p> </p>";
}

$conn->close();

?>  
  • 写回答

1条回答 默认 最新

  • doudou8893 2016-06-02 09:44
    关注
    $date = $_POST['date'] ;
    
    $date = date("Y-m-d",strtotime($date));
    

    Convert $_POST['date'] in Y-m-d format.

    To show date in Y-m-d format in HTML page, again convert it into

    $date = date("d-m-Y",strtotime($row['date']));

    OR

    echo date("d-m-Y",strtotime($row['date']));
    

    [Note: I'm assuming your fetch array as $row and column as date.]

    For more info, please have a look on strtotime - php manual

    Update Code (As question is edited)

    $sql = "SELECT * FROM table WHERE id='1' ORDER BY id DESC LIMIT 1";
    
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
      // output data of each row
      while($row = $result->fetch_assoc()) {
        echo "<td>" . $row["id"]. "</td>";
        echo "<td>" . date("d-m-Y",strtotime($row['date'])). "</td>";
      }                   
    } else {
      echo "<p> </p>";
    }
    
    $conn->close();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算