简体   繁体   English

PHP通过基于timeStamp和当前时间的MySQL返回结果

[英]PHP Return results via MySQL Based on timeStamp and the Current time

I'm writing a little bit of PHP, but Im a serious nOOb. 我在写一点PHP,但是我是认真的。

I want to compare the current time (The time now) with a timestamp in a table, stored like 1085225495. 我想将当前时间(现在的时间)与表中的时间戳进行比较,该表的存储方式为1085225495。

If the timestamp is under 1 hour old, then do: 如果时间戳小于1小时,请执行以下操作:

UPDATE TABLE SET visible=0;

Otherwise ignore. 否则忽略。

Any ideas in php? PHP中的任何想法?

What about something like this : 那么这样的事情呢:

update your_table
set visible = 0
where your_timestamp_field >= UNIX_TIMESTAMP(subdate(now(), interval 1 hour))


As explanation, here's a select that might help you : 作为说明,以下选择可能对您有帮助:

mysql> select now(), subdate(now(), interval 1 hour), UNIX_TIMESTAMP(subdate(now(), interval 1 hour))\G
*************************** 1. row ***************************
                                          now(): 2009-09-17 18:58:31
                subdate(now(), interval 1 hour): 2009-09-17 17:58:31
UNIX_TIMESTAMP(subdate(now(), interval 1 hour)): 1253203111
1 row in set (0.00 sec)

Here : 这里 :

  • now() gets the current date and time now()获取当前日期和时间
  • the subdate() gives 1 hour before now subdate()会比现在subdate() 1小时
  • and unix_timestamp() converts that to a unix timestamp unix_timestamp()将其转换为unix时间戳


You might also do a substraction of 3600 seconds on UNIX_TIMESTAMP(now()) , instead of using subdate... But I like the subdate call : I find it easier to immediatly understand that you want 1 hour (and not a magic number like 3600) 您可能还需要在UNIX_TIMESTAMP(now())上减去3600秒,而不是使用subdate ...但是我喜欢subdate调用:我发现更容易立即理解您想要1小时(而不是像这样的神奇数字) 3600)

Here's one way to do it entirely in MySQL 这是一种完全在MySQL中完成的方法

UPDATE foo SET visible=0 
WHERE (UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(timestamp))<3600;

UNIX_TIMESTAMP() gives you the number of seconds since the epoch, and subtracting the timestamp seconds from the current seconds gives you the age in seconds. UNIX_TIMESTAMP()为您提供自该纪元以来的秒数,并从当前秒数减去时间戳秒数可以得到以秒为单位的时间。

UPDATE TABLE SET visible = 0 WHERE myTimestamp >= UNIX_TIMESTAMP(CURTIME()) - 3600

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

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