在PHP中检查日期时间之间的间隔 [英] Check interval between datetimes in PHP

查看:53
本文介绍了在PHP中检查日期时间之间的间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有一个字段,该字段的类型为datetime,当用户访问页面时我在其中添加时间.当用户再次来访时,我想检查他的第一次访问和当前访问之间的间隔.如果少于或等于1小时,那么我想向他显示一些消息. 我这样存储时间

I have a field in database that has type of datetime in which I add time when user visit a page. When user again comes I want to check the interval between his first visit and current. If it is less or equal to 1 hour then I want to show him some message. I store time like this

2011-03-04 00:25:01

我想问的问题是如何检查PHP中的间隔

The thing that I want to ask that how to check the interval in PHP

推荐答案

如果您的PHP> = 5.3,则可以使用 DateTime 对象和功能:

If you have PHP >= 5.3 you can use DateTime objects and functions:

$visit = DateTime::createFromFormat('Y-m-d H:i:s', '2011-03-04 00:25:01');
$now = new DateTime("now");

$diff = $now->diff($visit);

这篇关于在PHP中检查日期时间之间的间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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