在MySQL PHP中更改日期和时间字段的显示格式 [英] Change display format of date and time field in MySQL PHP

查看:269
本文介绍了在MySQL PHP中更改日期和时间字段的显示格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中的select语句是

My select statement in PHP is

  "select * from table";

我使用以下PHP语句显示日期& MySQL字段的时间.

I use the following PHP statement to display date & time of MySQL field.

  <?php
      echo $row['mdate'];
  ?>

结果是这样的

  2010-03-09 16:59:18

我想以以下格式查看结果

I want to view the result in the following format

  09-03-2010 16:59:18

我想以以下格式查看结果

and I want to view the result in the following format

  09-03-2010 4:59:18 PM

,无需定义任何其他功能.我只能修改我的echo语句.

without defining any extra function. I can only modify my echo statement.

  <?php echo $row['msgdate']; ?>

     or

我还可以修改我的select语句.

I can also modify my select statement.

推荐答案

您可以直接在数据库中进行格式化,如Frank Heikens在其答案中所示,也可以在PHP中进行格式化.使用

You can do the formatting directly in the database as Frank Heikens shows in his answer, or you can do it in PHP. Convert the mySQL date value to a UNIX timestamp using

$timestamp = strtotime($row["mdate"]);

然后,您可以使用 date() 的所有选项对其进行格式化,例如:

then you can use all options of date() to format it, for example:

echo date("Y-m-d H:i:s", $timestamp); // returns 09-03-2010 16:59:18

两种方法都同样有效;我个人喜欢修改PHP中的值.

both approaches are equally valid; I personally like to modify the value in PHP.

这篇关于在MySQL PHP中更改日期和时间字段的显示格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆