/    Sign up×
Community /Pin to ProfileBookmark

date comparison

i’m creating a script that will tag members whenever they login with the last logon date, what’s the most reliable method of comparing the time that has elapsed since their last login?

important: the end user won’t see any of this, i want to be able to determine if they have logged in within 7 days of their last login date. so this will be a recurring check, not a one time check.

i intend to call time() and store it, and then when they login again get the new time().

so i’d end up with $prev_login and $cur_login for example.

but how would i subtract the old and new and get an integer representing how many days have elapsed?

so it would be something like this:

function DaysElapsed($cur_login,$prev_login){
$elapsed = $cur_login – $prev_login;
/* code to convert $elapsed to days */
return $elapsed;
}

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@phoenixbytesMar 09.2010 — i use this ere doofer

[code=php]

/////////////////////////////////////////////////////
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}

$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");

$now = time();
$unix_date = strtotime($date);

// check validity of date
if(empty($unix_date)) {
return "Bad date";
}

// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";

} else {
$difference = $unix_date - $now;
$tense = "from now";
}

for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}

$difference = round($difference);

if($difference != 1) {
$periods[$j].= "s";
}

return "$difference $periods[$j] {$tense}";
}
/////////////////////////////////////////////////////

[/code]


you use this with 2010-03-09 17:52:01 timestamp.

so, with this i store their lastactive time using now() then i'll call it up with

$lastactive = nicetime("2010-03-09 17:52:01");

and it spits out the time in seconds, minutes, days etc

"4 minutes ago"

so you could pull up their last active time just before you set it when they login, wrap this function around it, do what you plan to do with it then update their lastactive and call that if you so wish.
Copy linkTweet thisAlerts:
@MindzaiMar 09.2010 — [code=php]$days = floor((time() - $old) / 86400); // assumes $old is the last logged time()[/code]
Copy linkTweet thisAlerts:
@bsmbahamasauthorMar 10.2010 — i'll try midzai's first and then the longer one if need be.

thanks for both!!!
×

Success!

Help @bsmbahamas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.14,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...