[英]Timestamp older than current time if else statement
Im testing an online/offline function with timestamp from the database against DateTime NOW. 我用DateTime NOW从数据库测试带有时间戳的在线/离线功能。
But I cant figure out how to set interval time on lets say 5 minutes. 但是我不知道如何设置间隔时间,比如说5分钟。 If the user has not done anything in 5 minutes, appear offline.
如果用户在5分钟内未执行任何操作,则显示为脱机。
DateTime NOW will always be bigger than Database time. DateTime NOW将始终大于数据库时间。 Any ideas on how to set the interval time on the statement ?
关于如何在语句上设置间隔时间的任何想法吗?
This is what i have so far, 这就是我到目前为止
// $user_info['activity'] 2013-04-27 19:27:39 //
$dateTime = new DateTime("now", new DateTimeZone('Europe/Warsaw'));
if($user_info['activity']>$dateTime){
echo 'user Online';
}else{
echo 'user inactive';
}
Something liKe this might work 可能会起作用
$now = new DateTime(null, new DateTimeZone('Europe/Warsaw'));
$last_activity = new DateTime($user_info['activity']);
$last_activity->modify('+5 minutes');
if($now > $last_activity){
echo 'user inactive';
}else{
echo 'user online';
}
One possible solution is to change your database timestamp to be a DateTime object as well and then use the DateTime::diff() method for comparison. 一种可能的解决方案是将您的数据库时间戳也更改为DateTime对象,然后使用DateTime :: diff()方法进行比较。
$dateTime1 = new DateTime(strtotime($user_info['activity']), new DateTimeZone('Europe/Warsaw'));
$dateTime2 = new DateTime("now", new DateTimeZone('Europe/Warsaw'));
$interval = $dateTime1->diff($dateTime2);
This will allow you to determine the interval and do a comparison using it. 这将允许您确定间隔并使用该间隔进行比较。
Use DateTime::diff()
for this: 为此使用
DateTime::diff()
:
$now = new DateTime();
$activity = $user_info['activity'];
// get difference between the two dates
$difference = $now->diff($activity);
// ->i contains the minutes
if($difference->i >= 5) {
echo "logged out";
}
I did this recently, like this: 我最近这样做,就像这样:
$time = time();
$last_activity = //time from db
$time_since = $time - $last_activity
Then clean up $time_since
if necessary for your application. 然后根据需要清理
$time_since
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.