简体   繁体   English

时间戳查询不起作用

[英]timestamp query doesnt work

i have a timestamp problem.我有时间戳问题。 i would like to check a timestamp if that is in the past or future.我想检查时间戳是过去还是将来。 if it is in the past, a query should happens.如果是过去,则应该进行查询。

up to this point i got a mysql-timestamp that will be fetched by到目前为止,我得到了一个 mysql-timestamp,它将被获取

$row = $query->fetch_object();
$timestamp = $row->time

that contains:其中包含:

2012-04-04 12:06:38

for further settings i use two more variables which will be:对于进一步的设置,我使用了另外两个变量,它们是:

$added = $timestamp + 1209600;  //what adds 14days (1209600 seconds) to my db-timestamp
$check = $added < time();  //what checks if the 14days have passed or not

okay now if i test all of these with var_dump it will return for each:好吧,现在如果我用 var_dump 测试所有这些,它将返回每个:

string(19) "2012-04-04 12:06:38" at var_dump($timestamp)
int(1211612) at var_dump($added)
bool(true) at var_dump($check)

so my problem is, that the following query will be executed all the time, even in case of when the 14days should have been passed.所以我的问题是,下面的查询将一直执行,即使在 14 天应该过去的情况下也是如此。 i know that because i changed the timestamp to earlier date in january.我知道是因为我在 1 月份将时间戳更改为更早的日期。 here is my query:这是我的查询:

if ($check === true){
    $update = $db->query("UPDATE table SET xy='1' WHERE x='$x'");
    }

something seems to be wrong and i have no clue what that might be.似乎出了点问题,我不知道那可能是什么。

Looks to me like your trying to add a string and and int.在我看来,您正在尝试添加一个字符串和一个整数。

Maybe you should try converting the timestamp to and int before adding your constant to it.也许您应该在将常量添加到其中之前尝试将时间戳转换为和 int。

You can do it directly in the query when you are selecting the timestamp using for instance the TO_SECONDS() method:当您使用例如TO_SECONDS()方法选择时间戳时,您可以直接在查询中执行此操作:

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

I agree with barsju.我同意barsju。 The result from MySQL is a string of the DateTime entry and not actually a timestamp. MySQL 的结果是 DateTime 条目的字符串,实际上不是时间戳。 Another option that would keep it within PHP is strtotime($string);将其保持在 PHP 内的另一个选项是strtotime($string); that will translate the string into its timestamp.这会将字符串转换为其时间戳。 All that matters is that you get a timestamp before your check.重要的是你在检查之前得到一个时间戳。

I would do it on this line of your example:我会在您的示例的这一行中执行此操作:

$timestamp = strtotime($row->time);

I believe there are also a couple other ways to get MySQL to return the timestamp instead as well.我相信还有其他一些方法可以让 MySQL 返回时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM