在MySQL查询中将时间戳转换为日期

在MySQL查询中将时间戳转换为日期,mysql,sql,date-formatting,Mysql,Sql,Date Formatting,我想将MySQL中的时间戳转换为日期 我想将user.registration字段格式化为yyyy-mm-dd文本文件 以下是我的SQL: $sql = requestSQL("SELECT user.email, info.name, FROM_UNIXTIME(user.registration), info.news FROM

我想将MySQL中的
时间戳
转换为日期

我想将user.registration字段格式化为
yyyy-mm-dd
文本文件

以下是我的SQL:

$sql = requestSQL("SELECT user.email, 
                   info.name, 
                   FROM_UNIXTIME(user.registration),
                   info.news
                   FROM user, info 
                   WHERE user.id = info.id ", "export members");
我还尝试了以下日期转换:

DATE_FORMAT(user.registration, '%d/%m/%Y')
DATE(user.registration)
我在写入文本文件之前回显结果,得到:

电子邮件1;名称1;日期格式(user.registration,'%d/%m/%Y');新闻1

电子邮件2;名称2;新闻2


如何将该字段转换为日期?

若要仅获取日期,可以将其转换为日期

为了获得特定的格式,请使用


如果
注册
字段的类型确实是
时间戳
,您应该能够执行以下操作:

$sql = "SELECT user.email, 
           info.name, 
           DATE(user.registration), 
           info.news
      FROM user, 
           info 
     WHERE user.id = info.id ";

注册应该显示为yyyy-mm-dd

如果您在输出中获得查询,则需要向我们显示实际回显结果的代码。你能发布调用requeteSQL的代码吗

例如,如果您在php中使用了单引号,它将打印变量名,而不是值

echo 'foo is $foo'; // foo is $foo
这听起来很像你的问题,我肯定这就是原因

此外,请尝试删除@符号,看看这是否有助于提供更多信息

所以

$SQL_result = @mysql_query($SQL_requete); // run the query
变成

  $SQL_result = mysql_query($SQL_requete); // run the query
如果查询抛出错误,这将停止任何错误抑制。

在MYSQL中将时间戳转换为日期
DATE_FORMAT(FROM_UNIXTIME(`user.registration`), '%e %b %Y') AS 'date_formatted'
使用整数时间戳创建表格:

mysql> create table foo(id INT, mytimestamp INT(11));
Query OK, 0 rows affected (0.02 sec)
mysql> select id, from_unixtime(mytimestamp) from foo;
+------+----------------------------+
| id   | from_unixtime(mytimestamp) |
+------+----------------------------+
|    1 | 2013-10-08 16:07:28        |
+------+----------------------------+
1 row in set (0.00 sec)
mysql> select id, from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') from foo;
+------+-------------------------------------------------+
| id   | from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') |
+------+-------------------------------------------------+
|    1 | 2013 8th October 04:07:28                       |
+------+-------------------------------------------------+
1 row in set (0.00 sec)
插入一些值

mysql> insert into foo values(1, 1381262848);
Query OK, 1 row affected (0.01 sec)
看一看

mysql> select * from foo;
+------+-------------+
| id   | mytimestamp |
+------+-------------+
|    1 |  1381262848 |
+------+-------------+
1 row in set (0.00 sec)
将数字转换为时间戳:

mysql> create table foo(id INT, mytimestamp INT(11));
Query OK, 0 rows affected (0.02 sec)
mysql> select id, from_unixtime(mytimestamp) from foo;
+------+----------------------------+
| id   | from_unixtime(mytimestamp) |
+------+----------------------------+
|    1 | 2013-10-08 16:07:28        |
+------+----------------------------+
1 row in set (0.00 sec)
mysql> select id, from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') from foo;
+------+-------------------------------------------------+
| id   | from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') |
+------+-------------------------------------------------+
|    1 | 2013 8th October 04:07:28                       |
+------+-------------------------------------------------+
1 row in set (0.00 sec)
将其转换为可读格式:

mysql> create table foo(id INT, mytimestamp INT(11));
Query OK, 0 rows affected (0.02 sec)
mysql> select id, from_unixtime(mytimestamp) from foo;
+------+----------------------------+
| id   | from_unixtime(mytimestamp) |
+------+----------------------------+
|    1 | 2013-10-08 16:07:28        |
+------+----------------------------+
1 row in set (0.00 sec)
mysql> select id, from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') from foo;
+------+-------------------------------------------------+
| id   | from_unixtime(mytimestamp, '%Y %D %M %H:%i:%s') |
+------+-------------------------------------------------+
|    1 | 2013 8th October 04:07:28                       |
+------+-------------------------------------------------+
1 row in set (0.00 sec)

只需使用mysql的日期函数:

mysql> select DATE(mytimestamp) from foo;
尝试:


它将以毫秒为单位将时间戳格式化为yyyy mm dd字符串。

您应该将
时间戳
转换为
日期

select FROM_UNIXTIME(user.registration, '%Y-%m-%d %H:%i:%s') AS 'date_formatted'

我使用了“日期”功能,如中所述:

FROM_UNIXTIME(unix_timestamp,[format])
就是您所需要的一切

FROM_UNIXTIME(user.registration, '%Y-%m-%d') AS 'date_formatted'
FROM_UNIXTIME
获取一个数值,并将其转换为
日期
对象,
或者,如果给定一个格式字符串,它将作为字符串返回


旧的解决方案是获取初始日期对象,并使用第二个函数
date\u format
对其进行格式化。。。但这不再是必要的

它会像那样保存文件吗?您用来运行sql查询的代码是什么?我编辑了我的问题,requeteSQL正在运行查询。我尝试过,但得到了相同的结果:显示的是日期(user.registration),而不是有人建议的字段值。您是否能够直接使用mysql客户端成功运行查询?我没有听说过requeteSQL函数,它到底做什么?不幸的是,我不能直接使用mysql客户端,我没有自己的服务器。。。我提供了requeteSQL,基本上它只是运行查询和测试发送电子邮件是否出错……实际上我意识到我的字段是BIGINT类型,而不是时间戳。只有内容是一个时间戳(1328649722),所以它不起作用。现在一切都很好,从大学时代!它给出了相同的结果,显示的是cast(user.registration as date)而不是字段的值。这个工作虽然-cast(from_unixtime(user.registration)as date)我有特定的日期格式,它对我有效,并且可能感谢使用“as DATETIME”从MySQL 5.6.23获得小时分秒,我发现FROM UNIXTIME()的值应该直接输入:例如,日期格式(FROM UNIXTIME(user.registration),“%e%b%Y”)作为“日期格式”选择*来自日期格式的用户(FROM UNIXTIME(
用户
用户在
创建的用户),“%Y-%b-%e”)='2015-03-06'这是正确的格式还是我应该修改它?@DheerajThedijje这显示为2018-11-6。您可能正在寻找
“%Y-%m-%d”
,因为您要在PHP中格式化它
(日期('Y-m-d',$row->user_created_at))
-这(SQL和PHP两种变体)显示为2018-11-06如果您想以这种方式添加时间“hh:mm:ss”,请将其添加到格式字符串“%H:%i:%s”
选择日期(UNIX_TIMESTAMP())
spits
NULL
在我的MySQL 5.7.14中,请尝试日期(当前时间戳())将%h固定到%h