It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I wanted to know a good and efficient way to be able to tell how long ago my users last logged in.
On the users profile I want it for say how long ago their last log in was.
Eg:
User 1
Last login: 2 hours ago
User 2
Last login: 3 minutes ago
User 3
Last login: 2 months ago
I will keep their last login information in a MySQL database but want to know how to do the script.
I just realized that Stackoverflow uses this feature, so that can help you understand what I want.
mysql_query("UPDATE users SET lastactivity = ".time()." WHERE id = ".$userID);
This is how I will update the DB.
Each page request just update their last activity.
<?php
mysql_query("UPDATE users SET lastactivity = ".time()." WHERE id = ".$userID);
to show when they were last online, just select their lastactivity field from database and show it
<?php
$activity = mysql_result(mysql_query("SELECT lastactivity FROM users WHERE id = ".$userID), 0);
echo "Last activity: ".relativeTime($active);
where relativeTime() is function I've been using:
function relativeTime($time, $short = false){
$SECOND = 1;
$MINUTE = 60 * $SECOND;
$HOUR = 60 * $MINUTE;
$DAY = 24 * $HOUR;
$MONTH = 30 * $DAY;
$before = time() - $time;
if ($before < 0)
{
return "not yet";
}
if ($short){
if ($before < 1 * $MINUTE)
{
return ($before <5) ? "just now" : $before . " ago";
}
if ($before < 2 * $MINUTE)
{
return "1m ago";
}
if ($before < 45 * $MINUTE)
{
return floor($before / 60) . "m ago";
}
if ($before < 90 * $MINUTE)
{
return "1h ago";
}
if ($before < 24 * $HOUR)
{
return floor($before / 60 / 60). "h ago";
}
if ($before < 48 * $HOUR)
{
return "1d ago";
}
if ($before < 30 * $DAY)
{
return floor($before / 60 / 60 / 24) . "d ago";
}
if ($before < 12 * $MONTH)
{
$months = floor($before / 60 / 60 / 24 / 30);
return $months <= 1 ? "1mo ago" : $months . "mo ago";
}
else
{
$years = floor ($before / 60 / 60 / 24 / 30 / 12);
return $years <= 1 ? "1y ago" : $years."y ago";
}
}
if ($before < 1 * $MINUTE)
{
return ($before <= 1) ? "just now" : $before . " seconds ago";
}
if ($before < 2 * $MINUTE)
{
return "a minute ago";
}
if ($before < 45 * $MINUTE)
{
return floor($before / 60) . " minutes ago";
}
if ($before < 90 * $MINUTE)
{
return "an hour ago";
}
if ($before < 24 * $HOUR)
{
return (floor($before / 60 / 60) == 1 ? 'about an hour' : floor($before / 60 / 60).' hours'). " ago";
}
if ($before < 48 * $HOUR)
{
return "yesterday";
}
if ($before < 30 * $DAY)
{
return floor($before / 60 / 60 / 24) . " days ago";
}
if ($before < 12 * $MONTH)
{
$months = floor($before / 60 / 60 / 24 / 30);
return $months <= 1 ? "one month ago" : $months . " months ago";
}
else
{
$years = floor ($before / 60 / 60 / 24 / 30 / 12);
return $years <= 1 ? "one year ago" : $years." years ago";
}
return "$time";
}
In addition to what #Martin. said, you can get the current timestamp using time(), and then retrieve the last login timestamp from the database, and subtract it from the current timestamp.
<?php
$date = strtotime("2012-03-08 16:02:35");
$now = time();
$diff = $now - $date;
?>
...then $diff is the time difference in seconds. Then you can calculate whatever you want.
Related
This question already has answers here:
PHP: convert seconds to minutes and hours
(2 answers)
Closed 4 years ago.
Convert video time
$json_array = file_get_contents("https://graph.facebook.com/v2.12/$PID[$FID]/videos?fields=title,length,from,description,created_time,source&limit=10&access_token=");
$json_data=json_decode($json_array,true);
foreach($json_data['data'] as $links){
$video_time = $links['length'];
238.142 this time to convert 3.57
Here is the algorithm:
.142 is irrelevant as you only seem to want minutes and seconds
To calculate the seconds, calculate the modulo by 60
238 % 60 = 58
To calculate the minutes, remove the seconds from your total time and divide by 60
(238 - 58) / 60 = 3
=> 3 minutes and 58 seconds
For this you can simply use the gmdate() function,
echo gmdate("H:i:s", 238.142); //output: 00:03:58
echo gmdate("H.i.s", 238.142); //output: 00.03.58
echo gmdate("i.s", 238.142); //output: 03.58
function vtime($lenth){
$seconds = $lenth;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
if($hours == 0){ return "$minutes:$seconds"; }
elseif($minutes == 0){ return $seconds; }
else { return "$hours:$minutes:$seconds"; }
}
echo vtime('304');
Here is a PHP function I have designed, which is supposed to determine the amount of time passed in secs,mins,hours,days,weeks,months, and years - depending upon which bracket it is beneath. However Ive noticed that the values being returned seem to increase faster than actual time (within 30 mins, it outputs "1 weeks ago"). Im wondering if $time is actually in millis? That said, for the first 2 mins it outputs relatively accurate results.
Note: Running on 1and1 host.
Here's the function:
function get_relative_date($conn,$date,$post_id){
#get current timestamp
$q = "SELECT NOW() - INTERVAL 1 HOUR - `timestamp` FROM `posts` WHERE id=".$post_id.";";
# - INTERVAL 1 HOUR added due to server running 1 hour ahead.
$result = mysqli_query($conn,$q);
if ($result != null && $result != false){
$row = mysqli_fetch_array($result,MYSQLI_BOTH);
$time = $row[0]; #time passed since post
$min = 60;
$hour = $min*60;
$day = $hour*24;
$week = $day*7;
$month = $week*4;
$year = $month*12;
if ($time < $min){
return $time." secs ago";
}
if ($time < $hour){
return round(($time / $min))." mins ago";
}
if ($time < $day){
return round(($time / $hour))." hours ago";
}
if ($time < $week){
return round(($time / $day))." days ago";
}
if ($time < $month){
return round(($time / $week))." weeks ago";
}
if ($time < $year){
return round(($time / $month))." months ago";
}
if ($time >= $year){
return round(($time / $year))." years ago.";
}
return false;
}
}
Cheers.
I tried your query out and instead of 2 minutes and 40 seconds (in seconds) I was receiving 240 and instead of 1 hour 3 minutes and 40 seconds (in seconds) I got 10340.
You need this:
SELECT TIME_TO_SEC(TIMEDIFF(NOW(),`timestamp`)) FROM `posts` ...
This will select the time difference in seconds and hopefully give you the desired result.
I have a function to calculate the difference between two dates.
function getDateDifference($to, $from, $in) {
$diff = abs($to - $from);
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
if ($in == "days") {
return $days;
} else if ($in == "months") {
return $months;
} else if ($in == "years") {
return $years;
}
}
For the parameters i first convert the two dates into seconds like this,
checkin = '2012-07-26';
checkout = '2012-07-27';
check_in_date = strtotime(checkin);
check_out_date = strtotime(checkout);
im getting the correct difference when it comes to difference less than one month. But if the difference is more than one month, im always getting the difference as 1. Can someone tell me wat the problem is.
Currently, a month is always 30 * 60 * 60 * 24 sec, aka 30 days.
Your problem is that we're in July, and there are 31 days, not 30. You must take care of number of days per month.
You can make use of the DateTime class.
http://php.net/manual/en/datetime.diff.php
$checkin = new DateTime("2012-07-23");
$checkout = new DateTime("2012-07-27");
$difference = $checkin->diff($checkout);
echo "differrence = " . $difference->format('%R%a days');
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I've got a piece of code that I created and I've been 'requiring_once' in to my PHP code for quite a while now. It works fine on my local PC, but when I upload it to my server it's coming up with the following error:
Parse error: syntax error, unexpected T_ELSE in /home/makequiz/public_html/nitpicker/func.php on line 47
Here's the function:
function howLongAgo($unix_time) {
// This function shows the unix time stamp
// as number of seconds, minutes, hours, days, months, years ago
// Get the time difference between time specified and the current unix time
$time_difference = time () - $unix_time;
$seconds_ago = $time_difference;
$minutes_ago = $time_difference / 60;
$hours_ago = $time_difference / 3600;
$days_ago = $time_difference / 86400;
$months_ago = $time_difference / 2629743;
$years_ago = $time_difference / 31556926;
if ($time_difference < 60) {
// Seconds Ago
return round ( $time_difference ) . ' Seconds Ago';
} else if ( ($time_difference >= 60) && ($time_difference < 3600) ) {
// Minutes Ago
if (round ( $time_difference / 60 ) == 1) {
return round ( $time_difference / 60 ) . " Minute Ago";
} else {
return round ( $time_difference / 60 ) . " Minutes Ago";
}
} else if ($time_difference >= 3600 && $time_difference < 86400) {
// Hours Ago
if (round ( $time_difference / 3600 ) == 1) {
return round ( $time_difference / 3600 ) . " Hour Ago";
} else {
return round ( $time_difference / 3600 ) . " Hours Ago";
}
} else if ($time_difference >= 86400 && $time_difference < 2629743) {
// Days Ago
if (round ( $time_difference / 86400 ) == 1) {
return round ( $time_difference / 86400 ) . " Day Ago";
} else {
return round ( $time_difference / 86400 ) . " Days Ago";
}
} else if ($time_difference >= 2629743 && $time_difference < 31556926) {
// Months Ago
if (round ( $time_difference / 2629743 ) == 1) {
return round ( $time_difference / 2629743 ) . " Month Ago";
} else {
return round ( $time_difference / 2629743 ) . " Months Ago";
}
} else if ($time_difference >= 31556926) {
// Years Ago
if (round ( $time_difference / 31556926 ) == 1) {
return round ( $time_difference / 31556926 ) . " Year Ago";
} else {
return round ( $time_difference / 31556926 ) . " Years Ago";
}
}
}
This error is coming up, even though I haven't called the function at all in my code.
Can anyone see any errors in the code, cause I can't :(
This would be a lot simpler using a switch/case logic scheme. I have re-coded what you had up there using switch/case and this might be a good intro for you to learn switch/case. You had some extra ( ) in one of the if statements above. I hope this helps, friend:
<?
$unix_time = 6734;
echo howLongAgo($unix_time);
function howLongAgo($time_difference){
// Swtich logic based on the time difference passed to this function, sets the english string and what number the difference needs to be divided by
switch($time_difference){
case ($time_difference < 60):
$string = " second";
break;
case ($time_difference >= 60 && $time_difference < 3600):
$string = " minute";
$divider = 60;
break;
case ($time_difference >= 3600 && $time_difference < 86400):
$string = " hour";
$divider = 3600;
break;
case ($time_difference >= 86400 && $time_difference < 2629743):
$string = " day";
$divider = 86400;
break;
case ($time_difference >= 2629743 && $time_difference < 31556926):
$string = " month";
$divider = 2629743;
break;
case ($time_difference >= 31556926):
$string = " year";
$divider = 31556926;
break;
}
// If a divider value is set during the switch, use it to get the actual difference
if($divider){$diff = round($time_difference / $divider);}else{$diff = round($time_difference);}
// If the difference does not equal 1, pluralize the final result EG: hours, minutes, seconds
if($diff != 1){$pluralize="s";}
// Concatenate all variables together and return them
$final = $diff . $string . $pluralize . " ago";
return $final;
}
?>
I think your problem may come from the difference between elseif and else if (note the space).
From the PHP website:
Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
You have a syntax error in your code at line 47 (I am going to bet you are missing a semi-color or bracket at line 46.
It does not matter if you are calling the function or not, as the php parser will analyze the entire file upfront.
Couple of things..
did you change the file that it's included in when you loaded to your server?
try using include instead of require?
Convert 2010-04-16 16:30:00 to "Tomorrow Afternoon" or convert another date to "this afternoon", "next year", "next week wednesday". You get the picture.
Anyone know of a PHP or Javascript library that can do this?
I think you can come a long way with what is said here: Calculate relative time in C#
The logic is there, and it's not too hard to do the javascript equivalent if a solution in a different language suits you.
There might be more elegant solutions out there (look for Natural language formatting), but personally I couldn't find any.
I would suggest calculating the distance from now to the date you're formatting, and using thresholds.
Pseudo solution:
diff = now - date
if (diff < one_day)
format for today
if (diff < two_days)
format for tomorrow
if (diff < one_week)
format using days from now
.
.
.
The comparison will work for both past and future dates, as long as you use compare with the abs value of diff. Display timeunit ago or timeunit from now by checking if diff is positive or negative.
For the morning, afternoon, evening etc. you only need to check for the time of day in the date, and regarding the formatting type you hit, either display the time as numbers (far away), or natural language (recent or near date).
function gett($sam){
$times = time() - $sam;
if ($times == 60){
$times = "a minute ago";
}
if (($times != 1) && ($times < 60) && ($times != 0)){
$times = "$times seconds ago";
}
if ($times == 0){
$times = "less than a second ago";
}
if ($times == 1){
$times = "a second ago";
}
if ($times > 60 && $times < 3600){
$times = ceil($times/60)." minutes ago";
}
if($times == 3600){
$times = "an hour ago";
}
if($times > 3600 && $times < 86400){
$times = ceil($times/3600)." hours ago";
}
if($times == 86400){
$times = "a day ago";
}
if($times > 86400){
$times = ceil($times/86400)." days ago";
}
return $times; }
Usage:
$updated = gett($timestamp);
where $timestamp is pretty self-explanatory..
From this link -> How do I calculate relative time in C#?
function posted(t) {
var now = new Date();
var diff = parseInt((now.getTime() - Date.parse(t)) / 1000);
if (diff < 60) { return 'less than a minute ago'; }
else if (diff < 120) { return 'about a minute ago'; }
else if (diff < (2700)) { return (parseInt(diff / 60)).toString() + ' minutes ago'; }
else if (diff < (5400)) { return 'about an hour ago'; }
else if (diff < (86400)) { return 'about ' + (parseInt(diff / 3600)).toString() + ' hours ago'; }
else if (diff < (172800)) { return '1 day ago'; }
else {return (parseInt(diff / 86400)).toString() + ' days ago'; }
}