doulao2128 2012-05-15 04:42
浏览 36
已采纳

PHP / MySQL - 将DateTime显示为日期

I'm a little knew to SQL & PHP and have been given the task of displaying some information from the database. I know how to query my database and display the info into tables on screen using PHP and so forth; however this time I've been given a slightly different challenge.

I have information stored in the DateTime format in the SQL database and whilst retrieving it I need to strip the time and display only the date. I've had a read through many of the date/time functions for SQL but for some reason this seems to be going almost straight over my head. I've had a browse of a few sites including the two links below, but I'm having a hard time understanding how to do such things within PHP etc. If someone could steer me in the right direction that would be excellent!

2 somewhat related threads I've browsed: http://www.gfxvoid.com/forums/showthread.php?28576-PHP-Time-Date-Display http://blogs.x2line.com/al/archive/2006/02/17/1458.aspx

Logically, am I supposed to query the DateTime and then use PHP to reformat it the way I wish to display it? Or am I supposed to format the datetime using an SQL query?

Thanks very much!

  • 写回答

2条回答 默认 最新

  • dongpao1918 2012-05-15 04:50
    关注

    Just use the DATE function around the date time.

    SQL Column: created_at

    2012-05-09 13:46:25
    

    SELECT DATE_FORMAT(DATE(created_at), '%D %M %Y') FROM Table

    Returns:

    9th May 2012

    EDIT as per comment:

    To use it in PHP, you can do something like the following:

    Query: (Notice the AS clean_date)

    SELECT DATE_FORMAT(DATE(created_at), '%D %M %Y') AS clean_date FROM Table
    

    then in in php:

    <?php
    echo "<tr><td>{$row['clean_date']}</td>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题