PHP检查时间戳是否少于30分钟 [英] PHP Checking if timestamp is less than 30 minutes old

查看:867
本文介绍了PHP检查时间戳是否少于30分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取项目列表,每个项目都有一个CURRENT_TIMESTAMP,借助 timeago .这样就可以了.但是问题是我还希望在不到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分钟"),然后查看您行的时间戳是否大于该时间戳.

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.

这篇关于PHP检查时间戳是否少于30分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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