PHP检查时间戳是否小于30分钟

2022-01-13 00:00:00 timestamp php

我正在从我的数据库中获取一个项目列表,每个项目都有一个 CURRENT_TIMESTAMP,在 时间之前.所以这很好.但问题是我还想在不到 30 分钟的物品上贴上新"横幅.我如何获取生成的时间戳(例如:2012-07-18 21:11:12)并说明它是否距离当前时间不到 30 分钟,然后在该项目上回显NEW"横幅.

I'm getting a list of items from my database, each has a CURRENT_TIMESTAMP which i have changed into 'x minutes ago' with the help of timeago. So that's working fine. But the problem is i also want a "NEW" banner on items which are less than 30 minutes old. How can i take the generated timestamp (for example: 2012-07-18 21:11:12) and say if it's less than 30 minutes from the current time, then echo the "NEW" banner on that item.

推荐答案

使用strtotime("-30 minutes") 然后看看你的行的时间戳是否大于那个.

Use strtotime("-30 minutes") and then see if your row's timestamp is greater than that.

例子:

<?php
    if(strtotime($mysql_timestamp) > strtotime("-30 minutes")) {
        $this_is_new = true;
    }
?>

我在这里使用 strtotime() 两次来获取您的 mysql 日期的 unix 时间戳,然后再次获取 30 分钟前的时间戳.如果 30 分钟前的时间戳大于 mysql 记录的时间戳,那么它一定是在 30 分钟前创建的.

I'm using strtotime() twice here to get unix timestamps for your mysql date, and then again to get what the timestamp was 30 minutes ago. If the timestamp from 30 mins ago is greater than the timestamp of the mysql record, then it must have been created more than 30 minutes go.

相关文章