Jump to content

Time difference change over time


thefollower

Recommended Posts

Is there a way to print the time different depending on how long the time has been. So like if the user has not been logged in for 24 hours or more... it will say 1 day rather than 24 hours. And if its less than 1 hour it will say it in minutes... and if its greater than 1 hour but less than 1 day it will say minutes and hours?

 

This is my query which i have done so far but don't know how to do the above checking!

 

//get register date and now date
$GetLoginDate = mysql_query("SELECT DATEDIFF(NOW(), LoginDate) AS LastLogin FROM userregistration 
				WHERE  UserID='{$_SESSION['Current_User']}'")
				or die(mysql_error());
$lastlogin = mysql_fetch_assoc($GetLoginDate);

Link to comment
Share on other sites

Like this (keep in mind, 3600 is equal to an hour):

 

if ( $post_time => time()-3600*24 )
{
  $show = "1 Day ago";
  break;
}

 

or

 

if ( $post_time => time()-(3600*24)*7 )
{
  $show = "1 Week ago";
  break;
}

 

It's check if the post_time is equal or greater then the current time, minus 1 day (3600 seconds is 1 hour, so multiply by 24 makes 1 day)

 

You can do this for each "show" you want (1 hour, 1 day, 2 days, 1 week, 1 month etc) Just put it in a switch statement or something :)

Link to comment
Share on other sites

Yeh but that prints a fixed string meaning if a user had not logged in for like 60 days i'd have to make a huge case statement for all 60 days to do:

 

print '1 day ago';

print '2 days ago';

 

etc etc i was hoping to echo the returned value of the query like

 

 Echo $lastlogin['LastLogin']';Echo'Days ago!';

Link to comment
Share on other sites

Make a function that returns the time how you want.  basically you get the time in seconds of last login so something like

<?php
function time_display($last_login){
$now = date("U");
$dif = $now-$last_login;
if($diff  < 3600){$return = "1 Hours";}
elseif($dif <3600*2){$return = "2  Hours";}
elseif($diff <3600*10){$return = "10 Hours";}
elseif($diff < 3600*24){$return = "1 Day";}
else{$return = "More than 1 Day";}
return $return;
}
?>

Its not exact but will work

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.