MyBB Community Forums

Full Version: Last Post: X Minute(s)/Hour(s) Ago
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
Description: Small hack to display how many minute(s)/hour(s) past since the last post in forumdisplay and forumhome.

Open inc/functions.php
Add following code to bottom of the file before ?>
/**
 * Calculates how many minutes past since the given time.
 *
 * @param int The unix timestamp of the given time
 * @return string The formatted time
 */
function timeAgo($last)
{
	global $mybb, $mybbadmin, $lang;
	
	$timenow = TIME_NOW;
	$diff = $timenow - $last;
	
	if(!$offset && $offset != '0')
	{
		if($mybb->user['uid'] != 0 && array_key_exists("timezone", $mybb->user))
		{
			$offset = $mybb->user['timezone'];
			$dstcorrection = $mybb->user['dst'];
		}
		elseif(defined("IN_ADMINCP"))
		{
			$offset =  $mybbadmin['timezone'];
			$dstcorrection = $mybbadmin['dst'];
		}
		else
		{
			$offset = $mybb->settings['timezoneoffset'];
			$dstcorrection = $mybb->settings['dstcorrection'];
		}

		// If DST correction is enabled, add an additional hour to the timezone.
		if($dstcorrection == 1)
		{
			++$offset;
			if(my_substr($offset, 0, 1) != "-")
			{
				$offset = "+".$offset;
			}
		}
	}

	if($offset == "-")
	{
		$offset = 0;
	}
		
	if($last < $timenow && $diff <= 46200)
	{
		$order = $timenow - $last;
		while($order >= 60){
			$order = $order-60;
			$ordermleft++;
		}
		while($ordermleft >= 60){
			$ordermleft = $ordermleft-60;
			$orderhleft++;
		}

		if($ordermleft == 0){
			$ordermleft = "";
		} else {
			$ordermleft = "$ordermleft {$lang->minutes}";
		}
		if($orderhleft == 0){
			$orderhleft = "";
		} elseif($orderhleft == 1) {
			$orderhleft = "$orderhleft {$lang->hour}";
		} else {
			$orderhleft = "$orderhleft {$lang->hours}";
		}
		
		if(!$ordermleft || $ordermleft == 1){ $ordermleft = "1 {$lang->minute}"; }
		$actualtime = gmdate($mybb->settings['timeformat'], $last + ($offset * 3600));
		
		if($orderhleft)
		{
			$result = "<abbr title=\"$actualtime\">$orderhleft {$lang->df_ago}</abbr>";
		} else {
			$result = "<abbr title=\"$actualtime\">$ordermleft {$lang->df_ago}</abbr>";
		}
			
		return $result;
	} else {
		return gmdate($mybb->settings['timeformat'], $last + ($offset * 3600));
	}
}

Open inc/functions_forumlist.php
Find following:
$lastpost_date = my_date($mybb->settings['dateformat'], $lastpost_data['lastpost']);
$lastpost_time = my_date($mybb->settings['timeformat'], $lastpost_data['lastpost']);

Replace with following:
$lastpost_time = timeAgo($lastpost_data['lastpost']);
if(ereg($lang->minute, $lastpost_time) || ereg($lang->hour, $lastpost_time))
{
	$lastpost_date = "";
}
else
{
	$lastpost_date = my_date($mybb->settings['dateformat'], $lastpost_data['lastpost']);
}

Open forumdisplay.php
Find following:
$lastpostdate = my_date($mybb->settings['dateformat'], $thread['lastpost']);
$lastposttime = my_date($mybb->settings['timeformat'], $thread['lastpost']);

Replace with following:
$lastposttime = timeAgo($thread['lastpost']);
if(ereg($lang->minute, $lastposttime) || ereg($lang->hour, $lastposttime))
{
	$lastpostdate = "";
}
else
{
	$lastpostdate = my_date($mybb->settings['dateformat'], $thread['lastpost']);
}

Open your languages' global.lang.php file
Add following code to bottom of the file before ?>
$l['df_ago'] = "Ago";


That's all!

Screenshots:
[attachment=12241]
[attachment=12240]
Excellent, thank you!
This will stop me from using vbulletin finally...
wow Big Grin I'm going to use this
Very nice! Even better than mine!Sad But take note, this makes you lose the ability to use languages. So you need to make language variables $lang['minutes'], $lang['minute'], $lang['seconds'], $lang['second'], etc... That's only if you want to make it multilingual.
(2009-01-01, 05:46 PM)TomL Wrote: [ -> ]Very nice! Even better than mine!Sad But take note, this makes you lose the ability to use languages. So you need to make language variables $lang['minutes'], $lang['minute'], $lang['seconds'], $lang['second'], etc... That's only if you want to make it multilingual.
Your incorrect. My forum is in Hebrew (as I told again and again here) and it writes the time in Hebrew - not English.

DragonFever, Thanks. is works nicely.



Ori...
(2009-01-01, 05:52 PM)okitai Wrote: [ -> ]
(2009-01-01, 05:46 PM)TomL Wrote: [ -> ]Very nice! Even better than mine!Sad But take note, this makes you lose the ability to use languages. So you need to make language variables $lang['minutes'], $lang['minute'], $lang['seconds'], $lang['second'], etc... That's only if you want to make it multilingual.
Your incorrect. My forum is in Hebrew (as I told again and again here) and it writes the time in Hebrew - not English.

DragonFever, Thanks. is works nicely.



Ori...
Can I see a link to your forums, please?
I was sure that it is around here somewhere (I wrote the address so many times, already): http://www.ithelp.co.il/forum
the forum is opened for viewing (and yes, this is my true picture).

Edit:
So... I was wrong (at least, partially). Dates longer then 'today' are in Hebrew but, if its shorter then it was in English. I did have to change the code (a bit).
Is there a way to "pretty up" the time stamp? If the time is longer then 24 hours, I get something like "Yesterday 12:01". Is there a way to turn it to (for ex.) "Yesterday, at 12:01"?


Ori...
(2009-01-01, 05:46 PM)TomL Wrote: [ -> ]Very nice! Even better than mine!Sad But take note, this makes you lose the ability to use languages. So you need to make language variables $lang['minutes'], $lang['minute'], $lang['seconds'], $lang['second'], etc... That's only if you want to make it multilingual.

Actually, you're right. I'll do it and update the modification.
I was gonna' say, I knew I was right:p lol Can't wait till ya update it thoughSmile
(2009-01-01, 06:25 PM)I Wrote: [ -> ]Is there a way to "pretty up" the time stamp? If the time is longer then 24 hours, I get something like "Yesterday 12:01". Is there a way to turn it to (for ex.) "Yesterday, at 12:01"?

Please reply. This is, somewhat, important.


Ori...
Pages: 1 2 3 4 5 6