PHP-MYSQL:将Unix时间戳转换为DateTime,反之亦然 [英] PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa

查看:116
本文介绍了PHP-MYSQL:将Unix时间戳转换为DateTime,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用php 5.4.6和MySQL 5.5.29,我将UNIX TIMESTAMP转换为MYSQL DATETIME,反之亦然。 Mysql和php wun在同一本地机器上。



我有一个简单的mysql表

 code> CREATE TABLE条目(
id SERIAL PRIMARY KEY,
created DATETIME NOT NULL);

要插入数据,我使用php PDO和mysql NOW()。这个工作正常,正确的日期时间存储在数据库中。



我的计划是在客户端使用unix时间戳(服务器端的php& mysql) / p>

所以我想提供unix时间戳给我的客户端。
因此,我使用MySql UNIX_TIMESTAMP()函数直接在查询中转换。



所以一个示例查询如下所示:

  SELECT created,UNIX_TIMESTAMP(created)AS TS FROM条目

结果:
created = 2013-02-14 20:47:35
TS = 1360871255



所以现在我想做另一种方式,我通过一个UNIX时间戳,并希望与我的数据库中的条目进行比较。不幸的是,我无法编写一个有用的PHP脚本。我不知道为什么,但是当我将相同的时间戳(1360871255)传递给PHP时,我不会用这种方法得到2013-02-14 20:47:35:



公共静态函数toDateTime($ unixTimestamp){
返回日期(Ymd H:m:s,$ unixTimestamp);

  
}

当我打电话给DateTime(1360871255)将返回2013-02-14 20: 02:35这不是原来的DateTime。



我知道,我不需要格式化1360871255到Ymd H:m:s来使用它在MYSQL,但1360871255似乎不是我预期的时间(和MYSQL UNIX_TIMESTAMP已经返回)。



我想做的是一个简单的查询,显示我比某些时间戳,这样简单:

  SELECT * FROM条目WHERE created< 1360871255 

但正如我之前提到的,查询结果不是预期的,因为1360871255似乎不是正确的时间。



我没有为php中的mysql连接指定任何特殊的时区。



任何建议?

解决方案

您的日期格式是错误的... 是分钟,而不是 m (月)。 / p>

 返回日期(Ymd H:i:s,$ unixTimestamp); 

几个备注:




  • 没有必要重新分配,即 $ unixTimestamp = $ unixTimestamp;

  • 由于您使用PHP> 5.3。您可能对新的 DateTime 对象感兴趣。


im using php 5.4.6 and MySQL 5.5.29 and I get trouble by converting a UNIX TIMESTAMP to MYSQL DATETIME and vice versa. Mysql and php wun on the same local machine.

I have a simple mysql table

CREATE TABLE Entry(
id SERIAL PRIMARY KEY,
created DATETIME NOT NULL);

To insert data I use php PDO and mysql NOW(). This works fine, the correct datetime is stored in the database.

My plan is to work with unix timestamps on client side (php & mysql on server side).

So I would like to deliver unix timestamps to my client. Therefore I use MySql UNIX_TIMESTAMP() function to convert it directly in the query.

So a sample query looks like this:

SELECT created, UNIX_TIMESTAMP(created) AS TS FROM Entry

The result: created = 2013-02-14 20:47:35 TS = 1360871255

So now I want to do the other way, I pass the a UNIX Timestamp, and want to compare it with Entries in my Database. Unfortunetly Im not able to write a PHP script that works. I don't know why, but when I m passing the same timestamp (1360871255) to PHP I do not get 2013-02-14 20:47:35 with this method:

public static function toDateTime($unixTimestamp){
    return date("Y-m-d H:m:s", $unixTimestamp);
}

When I call toDateTime(1360871255) will return 2013-02-14 20:02:35 which is not the original DateTime.

I know, I dont need to format 1360871255 to a Y-m-d H:m:s to use it in MYSQL, but 1360871255 seems not to be the time that I expected (and MYSQL UNIX_TIMESTAMP has returned).

What I want to do is a simple query that shows me Entries that are older than a certain timestamp, something simple like this:

SELECT * FROM Entry WHERE created < 1360871255

but as I mentioned before, the query result is not the expected, because 1360871255 seems not to be the correct time.

I do not specify any special timezone for the mysql connection in php.

Any suggestions?

解决方案

Your date format is wrong... i is for minute, not m (months).

return date("Y-m-d H:i:s", $unixTimestamp);

A few side notes:

  • There's no need to re-assign, i.e. $unixTimestamp = $unixTimestamp;
  • Since you're using PHP > 5.3. you may be interested in the new DateTime object.

这篇关于PHP-MYSQL:将Unix时间戳转换为DateTime,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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