mysql - 显示 mysql 数据库 DATETIME 中的时间

标签 mysql date datetime time strtotime

我正在尝试显示数据库中的日期和时间。时间的问题是它总是在改变它的值并且与数据库中存储的值不匹配。

PHP 代码:

$query = mysql_query("SELECT `post_date`
                      FROM `private_section_posts`
                      WHERE `status` = 'show'");
while($post = mysql_fetch_array($query)) {

    $postdayofweek = date("D", strtotime($post['post_date']));
    $postday = date("d", strtotime($post['post_date']));
    $postsuffix = date("S", strtotime($post['post_date']));
    $postmonth = date("M", strtotime($post['post_date']));
    $postyear = date("Y", strtotime($post['post_date']));
    $posttime = date("g:ia", strtotime($post['post_date']));
    echo $postdayofweek . ' ' . $postday . $postsuffix . ' ' . $postmonth . ' ' . $postyear . 
    ' at ' . $time;

}

MYSQL post_date: 2014-01-28 04:00:00

输出显示:2014 年 1 月 28 日星期二上午 10:36

最佳答案

在MySQL中你可以这样尝试

SELECT DATE_FORMAT(post_date,'%a %D %b %Y at %l:%i%p') FROM `private_section_posts`  WHERE `status` = 'show'

示例

SELECT DATE_FORMAT(NOW(),'%a %D %b %Y at %l:%i%p');
+---------------------------------------------+
| DATE_FORMAT(NOW(),'%a %D %b %Y at %l:%i%p') |
+---------------------------------------------+
| Tue 28th Jan 2014 at 5:19PM                 |
+---------------------------------------------+
1 row in set (0.00 sec)

关于mysql - 显示 mysql 数据库 DATETIME 中的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404613/

相关文章:

PHP 类实例写入 MySQL 数据库

java - 如何测试应返回特定日期的日期函数

mysql - 在 mySql 中更新日期

python - 如何验证字符串是否正确的日期格式

c# - 给定两个日期时,检查在一个时间范围内是否花费了至少两个小时

JavaScript 日期怪癖

php - 在 $_POST 中重用 $_GET 变量?

mysql - Hibernate 更新 OneToMany

mysql - SQL 查找日期早于 x 的所有条目中的最高日期

Mysql为什么12.3 < 12.3是真的