• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.1 How to change the post date format?

Luxus

Well-known member
#1
I would like to add the exact time to the time format when a post has been posted.

Feb 27, 2011, 12:30

If you hover the date with your mouse you can see that the exact date is recorded.
This is the code for the date in threads:

HTML:
<a href="{xen:link threads/post-permalink, $thread, 'post={$post}'}" title="{xen:phrase permalink}" class="datePermalink"><xen:datetime time="$post.post_date" /></a>
I have found the phrase "date_x_at_time_y" which is used for the time format when you hover the post date, but I haven't figured where it comes from.

It seems that <xen:datetime time="$post.post_date" /> produces this:
HTML:
<span title="Feb 27, 2011 at 8:48 AM" class="DateTime">Feb 27, 2011</span>
This is the line that produces the visible post date of posts (without the time).
So I would like to know how I can edit this line to include the "date_x_at_time_y" phrase.
Thank you.
 

Jake Bunce

XenForo moderator
Staff member
#2
This can be achieved with a file edit:

library/XenForo/Template/Helper/Core.php

Change date to string:

Code:
	public static function helperDateTimeHtml($timestamp, $attributes = array())
	{
		$class = (empty($attributes['class']) ? '' : ' ' . htmlspecialchars($attributes['class']));

		unset($attributes['time'], $attributes['class']);

		$attribs = self::getAttributes($attributes);

		$time = XenForo_Locale::dateTime($timestamp, 'separate', self::$_language);

		if ($time['relative'])
		{
			$tag = 'abbr';
			$data = ' data-time="' . $timestamp . '" data-diff="' . (XenForo_Application::$time - $timestamp)
				. '" data-datestring="' . $time['date'] . '" data-timestring="' . $time['time'] . '"';
			$value = $time['string'];
		}
		else
		{
			$tag = 'span';
			$data = ' title="' . $time['string'] . '"'; // empty this to remove tooltip from non-relative dates
			$value = $time['date'];
		}

		return "<{$tag} class=\"DateTime{$class}\"{$attribs}{$data}>{$value}</{$tag}>";
	}
Or a template edit:

Admin CP -> Appearance -> Templates -> post

Replace the red code:

Code:
				<span class="item muted">
					<xen:username user="$post" class="author" />,
					<a href="{xen:link threads/post-permalink, $thread, 'post={$post}'}" title="{xen:phrase permalink}" class="datePermalink"><xen:datetime time="$post.post_date" /></a>
				</span>
Like so:

Code:
				<span class="item muted">
					<xen:username user="$post" class="author" />,
					<a href="{xen:link threads/post-permalink, $thread, 'post={$post}'}" title="{xen:phrase permalink}" class="datePermalink"><span class="DateTime">{xen:datetime {$post.post_date}, 'absolute'}</span></a>
				</span>
This works in my testing, but it loses the relative times (e.g. "a moment ago") which are handled nicely in the original function. So if you want to keep the relative times then the file edit is best.
 

Luxus

Well-known member
#3
Thank you very much, I'll go with the file edit then. Is there a rule that says when the time is displayed? Because I saw thread posts with post dates including the time and without the time.
 

Jake Bunce

XenForo moderator
Staff member
#4
Thank you very much, I'll go with the file edit then. Is there a rule that says when the time is displayed? Because I saw thread posts with post dates including the time and without the time.
If it's within the last week then it will be a relative date/time (e.g. "a moment ago", "18 minutes ago", "Yesterday at 7:04 PM", "Saturday at 10:55 AM"). Older than a week and it will show the actual date (e.g. "Jun 5, 2012"). But with the file edit I posted it affects those dates older than a week by making them show the full date/time (e.g. "Jun 12, 2012 at 11:44 AM").