简体   繁体   English

SQL查询不能使用DATE_FORMAT方法在PHP文件上重新格式化日期吗?

[英]SQL Query is not reformatting date on PHP file using DATE_FORMAT method?

By default, PHPMyAdmin has the datetime column formatted as YYYY-MM-DD, but I'm trying to reformat it into MM-DD-YYYY using an SQL statement on my PHP file. 默认情况下,PHPMyAdmin的datetime列格式为YYYY-MM-DD,但是我正在尝试使用PHP文件上的SQL语句将其格式化为MM-DD-YYYY。 This is the code I used, but it's not working: 这是我使用的代码,但是不起作用:

<?php
$usertext = $_POST['textinput'];
$insertsql = "INSERT INTO testtext (usertext) VALUES ('$usertext')";

if (isset($_POST['textinput'])) {
    require '../db.php';
    mysqli_query($db, $insertsql);
}

$selectdate = "SELECT textdate, DATE_FORMAT(textdate,'%m/%d/%Y') AS newdate FROM testtext";
$dateresult = mysqli_query($db, $selectdate);

while ($row = mysqli_fetch_assoc($dateresult)) {
    echo "{$row['textdate']}<br>";
}
?>

This code is able to display rows from the 'textdate' column onto my test site just fine, but the DATE_FORMAT code was unable to reformat the date. 这段代码可以很好地显示“文本日期”列中到我的测试站点上的行,但是DATE_FORMAT代码无法重新格式化日期。 Can you please help me fix this code? 您能帮我解决此代码吗? Thanks so much. 非常感谢。

You are calling your formatted date newdate but are referring to textdate in your code. 你在呼唤你的格式化的日期newdate但指textdate在你的代码。 You need to refer to that alias you assigned the results of DATE_FORMAT() to and not the original column identifier. 您需要引用将DATE_FORMAT()的结果分配给的别名,而不是原始的列标识符。

while ($row = mysqli_fetch_assoc($dateresult)) {
    echo "{$row['newdate']}<br>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM