简体   繁体   English

MySQL Date_Format无法使用

[英]MySQL Date_Format isn't working with

I have a PHP page with a MySQL database with 2 fields that use the TIME type in MySQL. 我有一个PHP页面,其中包含一个MySQL数据库,其中包含2个在MySQL中使用TIME类型的字段。 I want to convert those two fields from the 24 hour format (00:00:00) into 12 hour AM/PM format (00:00 AM) using MySQL's DATE_FORMAT('','') but it's not working. 我想使用MySQL的DATE_FORMAT('','')将这两个字段从24小时格式(00:00:00)转换为12小时AM / PM格式(00:00 AM) DATE_FORMAT('','')但是它不起作用。

So far, what I have done is created a 3rd and 4th field that also uses the TIME type. 到目前为止,我所做的是创建了一个第三和第四字段,该字段也使用TIME类型。 I send the 1st and 2nd field into the 3rd and 4th and convert the 3rd and 4th to preserve the original. 我将第一和第二字段发送到第三和第四字段,然后将第三和第四字段转换为原始格式。

<?php
//connection statements omitted

$sql="SELECT * FROM $table ORDER BY date LIMIT $start, $amount";
$result = $mysqli->query($sql);

//Sends the original time_in data to the new format_in column. 
$sql2="UPDATE $table SET format_in = time_in";
$result2=$mysqli->query($sql2);

$sql3="SELECT DATE_FORMAT(format_in,'%l:%i %p') FROM $table";
$result3=$mysqli->query($sql3);

while($row = $result->fetch_array()){

?>
//other fields omitted
<td><?php echo $row['format_in'];?></td>

//end while loop
<?php } ?>

The only thing this code does is replicates whatever was in the time_in column. 该代码唯一要做的就是复制time_in列中的内容。 Which is the standard 24 hour format 00:00:00 . 这是标准的24小时格式00:00:00 Basically, this code doesn't do anything. 基本上,此代码不执行任何操作。 What am I doing wrong here? 我在这里做错了什么?

EDITED to show my $result 编辑以显示我的$ result

while($row = $result3->fetch_array()){

instead of 代替

while($row = $result->fetch_array()){

Based on the conversation we had in comments section: 基于我们在评论部分的对话:

Change 更改

$sql="SELECT * FROM $table ORDER BY date LIMIT $start, $amount";
$result = $mysqli->query($sql);

to

$sql="SELECT field_1,field_2,field_n,DATE_FORMAT(format_in,'%l:%i %p') format_in FROM $table ORDER BY date LIMIT $start, $amount";
$result = $mysqli->query($sql);

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

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