比较sql时间戳和php日期的计算(“y-m-d h:i:s”)

时间:2021-08-06 21:28:39

Is there a way to see if ((the current time - a timestamp in a database)>10 minutes) in php? I have a row in a database called lockout. I have the following code so far:

在php中是否有一种方法可以查看(当前时间-数据库中的时间戳)>10分钟)?我在一个名为“锁定”的数据库中有一行。到目前为止,我有以下代码:

echo $row['lockout'];//returns 2013-05-12 22:04:17
echo date("Y-m-d H:i:s")//returns 2013-05-12 22:06:32    

Furthermore does anyone have any ideas why when I insert the current Timestamp in my database, it is about a half hour off, but has the right date and year?

此外,有人知道为什么当我在数据库中插入当前的时间戳时,会有大约半小时的间隔,但是有正确的日期和年份?

5 个解决方案

#1


2  

The SQL answer could depend on which SQL technology you use. To do this in PHP rather than SQL you could use the diff() method of DateTime:

SQL答案可能取决于您使用的SQL技术。要在PHP而不是SQL中实现这一点,可以使用DateTime的diff()方法:

$datetime1 = new DateTime($row['lockout']);
$datetime2 = new DateTime(date("Y-m-d H:i:s"));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%i minutes');

#2


1  

In PHP you can use strtotime function with relative formats like - 10 minutes to get the desired Unix timestamp:

在PHP中,您可以使用strtotime函数和相对格式(比如10分钟)来获得所需的Unix时间戳:

if ( strtotime('- 10 minutes') < strtotime( $row['lockout'] ) ) ...

#3


0  

Use the http://www.php.net/manual/en/book.datetime.php datetime class in php This has a diff method

使用php中的http://www.php.net/manual/en/book.datetime.php datetime类

#4


0  

If your SQL dialect is MySQL use

如果您的SQL方言是MySQL使用

... WHERE UNIX_TIMESTAMP(NOW())-lockout>=600 ...

or something similar in PHP.

或者类似的PHP。

#5


0  

Do this in your query:

在查询中执行以下操作:

WHERE `lockout` < DATE_SUB(NOW(), INTERVAL 10 MINUTE)

Or, if you cannot rely on MySQL's time, use

或者,如果您不能依赖MySQL的时间,请使用

mysqli_query('
 ... WHERE `lockout` < DATE_SUB(FROM_UNIXTIME(' . time() . '), INTERVAL 10 MINUTE) ...
');

Or, if you are bound to date(...), use

或者,如果你一定要约会(…),使用

mysqli_query('
 ... WHERE `lockout` < DATE_SUB(' . date('Y-m-d H:i:s') . ', INTERVAL 10 MINUTE) ...
');

You might want to compare MySQL's and PHP's timezones and your servers' ones (if MySQL and PHP live on separate hosts).

您可能需要比较MySQL和PHP的时区和服务器的时区(如果MySQL和PHP位于独立的主机上)。

#1


2  

The SQL answer could depend on which SQL technology you use. To do this in PHP rather than SQL you could use the diff() method of DateTime:

SQL答案可能取决于您使用的SQL技术。要在PHP而不是SQL中实现这一点,可以使用DateTime的diff()方法:

$datetime1 = new DateTime($row['lockout']);
$datetime2 = new DateTime(date("Y-m-d H:i:s"));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%i minutes');

#2


1  

In PHP you can use strtotime function with relative formats like - 10 minutes to get the desired Unix timestamp:

在PHP中,您可以使用strtotime函数和相对格式(比如10分钟)来获得所需的Unix时间戳:

if ( strtotime('- 10 minutes') < strtotime( $row['lockout'] ) ) ...

#3


0  

Use the http://www.php.net/manual/en/book.datetime.php datetime class in php This has a diff method

使用php中的http://www.php.net/manual/en/book.datetime.php datetime类

#4


0  

If your SQL dialect is MySQL use

如果您的SQL方言是MySQL使用

... WHERE UNIX_TIMESTAMP(NOW())-lockout>=600 ...

or something similar in PHP.

或者类似的PHP。

#5


0  

Do this in your query:

在查询中执行以下操作:

WHERE `lockout` < DATE_SUB(NOW(), INTERVAL 10 MINUTE)

Or, if you cannot rely on MySQL's time, use

或者,如果您不能依赖MySQL的时间,请使用

mysqli_query('
 ... WHERE `lockout` < DATE_SUB(FROM_UNIXTIME(' . time() . '), INTERVAL 10 MINUTE) ...
');

Or, if you are bound to date(...), use

或者,如果你一定要约会(…),使用

mysqli_query('
 ... WHERE `lockout` < DATE_SUB(' . date('Y-m-d H:i:s') . ', INTERVAL 10 MINUTE) ...
');

You might want to compare MySQL's and PHP's timezones and your servers' ones (if MySQL and PHP live on separate hosts).

您可能需要比较MySQL和PHP的时区和服务器的时区(如果MySQL和PHP位于独立的主机上)。