MyBB Community Forums

Full Version: Relative Date/Time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok, so here's the problem. Well, not really a problem, just a lack of information. You know on the forum how it lists the last post in that thread and it changes it depending on well, the last post made in that thread and the current date. What I am looking for is information on how to do this.

How would I save the date of a post in an appropriate format (which I've determined should be a UNIX timestamp), and how would I display whether the last post was "Today", "Yesterday", or if the last post was older then two days, simply display the date of the last post.

Any help is greatly appreciated. Perhaps which MyBB files handle this?

I post this question here, well, mainly because MyBB forums do this.

Thanks.

EDIT: Sorry. Posted this in the wrong section. Please feel free to move it to this board for example. Thanks.
Just use the my_date() function, eg
$friendlydate = my_date($unix_tiimestamp);
ZiNga BuRgA Wrote:Just use the my_date() function, eg
$friendlydate = my_date($unix_tiimestamp);

Thanks for your reply. However, I am not looking for a MyBB function. I am looking for the heart of it, the PHP coding. Where would I find the code that handles $friendlydate and whatnot.

Take Care.
my_date() is a customized MyBB Only? function of date().

./inc/functions.php
/**
 * Turn a unix timestamp in to a "friendly" date/time format for the user.
 *
 * @param string A date format according to PHP's date structure.
 * @param int The unix timestamp the date should be generated for.
 * @param int The offset in hours that should be applied to times. (timezones)
 * @param int Whether or not to use today/yesterday formatting.
 * @return string The formatted timestamp.
 */
function my_date($format, $stamp="", $offset="", $ty=1)
{
...
}
LeX- Wrote:my_date() is a customized MyBB Only? function of date().

./inc/functions.php
/**
 * Turn a unix timestamp in to a "friendly" date/time format for the user.
 *
 * @param string A date format according to PHP's date structure.
 * @param int The unix timestamp the date should be generated for.
 * @param int The offset in hours that should be applied to times. (timezones)
 * @param int Whether or not to use today/yesterday formatting.
 * @return string The formatted timestamp.
 */
function my_date($format, $stamp="", $offset="", $ty=1)
{
...
}

my_date is just the name MyBB gave that function. By all means what that function does is not MyBB only. They could have named is president_bush_is_an_idiot if they felt like it.

While you are getting sligthly closer, your not quite there yet. You would have to go deeper. Where do they define $format, $stamp and $ty?

Where is the specific code that grabs the last post date and compares it to the current date and then either displayes today, yesterday or the last date, as the last date something was posted.

Perhaps this is a question better suited for the MyBB developers.

While I appreciate your responses, I do not think your are understanding what I am trying to get at. Im sure I will find my answer soon enough.

EDIT: This is not necessarily relating to MyBB, it's more a question of how the developers of MyBB did something. I mean, I supposed I could have asked any company, such as PHPBB, or SMF, (they all display the last posted date in the same 'friendly' way but since I myself use MyBB, i figured I may as well ask you guys.

Thanks for all your help. I apreciate it.

Take care.
Merry Christmas.
$format is usually passed from $mybb->settings['dateformat'] and $stamp would be the UNIX timestamp

$ty is simply a 0/1 integer passed to the function

if($mybb->settings['dateformat'] == $format && $ty)
	{
		$stamp = time();
		$todaysdate = gmdate($format, $stamp + ($offset * 3600));
		$yesterdaysdate = gmdate($format, ($stamp - 86400) + ($offset * 3600));
		if($todaysdate == $date)
		{
			$date = $lang->today;
		}
		elseif($yesterdaysdate == $date)
		{
			$date = $lang->yesterday;
		}
	}
$format, $stamp, and $ty are parameters for a function, that is, they are variable inputs. When you 'use' the function, you define the $format, $stamp, and $ty, and pass it into the function, which does something with it, and pops out the result.

Why we chose to use $format, $stamp, and $ty? The names are purely arbitrary, but a better question is what do the three parameters do? my_date is based on the PHP date function. Because the PHP date function defines $format and $timestamp for the input to their function, we have to have at least $format and $stamp in our my_date function in order to 'use' PHP's date function. The $ty parameter in the my_date function is extra to implement the "Today" and "Yesterday" phrases.

So the programmer who 'uses' the my_date function has to ultimately 'define' the values of $format, $stamp, and $ty (optionally).