Tip for SMF 2.0/2.1: Show 'Date Joined' and 'Last Active' in posts and PMs

Started by GL700Wing, July 19, 2022, 11:40:18 PM

Previous topic - Next topic

GL700Wing

This tip is in response to requests for information on how to show the registration date (ie, 'Date Joined') and last login date (ie, 'Last Active') in the member profile section for posts and PMs.

Notes:
1. To keep the dates as compact as possible the 'Date Joined' and 'Last Active' dates are shown in the 'MMM YYYYY' format - if you want to use a different date format you will need to modify the relevant line(s) of code in ./Sources/Load.php;
2. 'Last Active' will only be displayed if the member allows others to see their online status or if the member viewing the post/PM is an admin.



In ./Sources/Load.php
Find:
        'registered_timestamp' => empty($profile['date_registered']) ? 0 : $profile['date_registered'],
Add After:
        // Tip/Trick: Show 'Date Joined' and 'Last Active'.
        'date_joined' => empty($profile['date_registered']) ? '' : timeformat($profile['date_registered'], '%b %Y'),
        'last_active' => empty($profile['last_login']) ? $txt['never'] : timeformat($profile['last_login'], '%b %Y'),


In ./Themes/default/Display.template.php
Find:
        // Any custom fields to show as icons?
Add Before:
        // Tip/Trick: Show 'Date Joined' and 'Last Active'.
        // Show when they joined.
        if (!empty($message['member']['date_joined']))
            echo '
                                <li>', $txt['dateJoined'], $message['member']['date_joined'], '</li>';

        // Tip/Trick: Show 'Date Joined' and 'Last Active'.
        // Maybe also show when they were last active.
        if (allowedTo('admin_forum') || empty($message['member']['is_hidden']))
            echo '
                                <li>', $txt['lastActive'], $message['member']['last_active'], (!empty($message['member']['is_hidden']) ? ' (' . $txt['hidden'] . ')' : ''), '</li>';


In ./Themes/default/PersonalMessage.template.php
Find:
        // Any custom fields to show as icons?
Add Before:
        // Tip/Trick: Show 'Date Joined' and 'Last Active'.
        // Show when they joined.
        if (!empty($message['member']['date_joined']))
            echo '
                                <li>', $txt['dateJoined'], $message['member']['date_joined'], '</li>';

        // Tip/Trick: Show 'Date Joined' and 'Last Active'.
        // Maybe also show when they were last active.
        if (allowedTo('admin_forum') || empty($message['member']['is_hidden']))
            echo '
                                <li>', $txt['lastActive'], $message['member']['last_active'], (!empty($message['member']['is_hidden']) ? ' (' . $txt['hidden'] . ')' : ''), '</li>';



In ./Themes/default/languages/Modifications.english.php
Add to end of file:

// Tip/Trick: Show 'Date Joined' and 'Last Active'.
$txt['dateJoined'] = 'Date Joined: ';
$txt['lastActive'] = 'Last Active: ';



Note: Code updated on 02-May-24 to correct issue reported by davo88
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Steve

Nice GL!  ;D

Also looks like a good candidate for a mod.  :)

One of the coders will need to look at this before it can be approved.
DO NOT pm me for support!

GL700Wing

Quote from: Steve on July 20, 2022, 07:47:29 AMNice GL!  ;D
Thanks - was quick and easy to do.

QuoteAlso looks like a good candidate for a mod.  :)
Maybe but it is a fairly simple code change and if I made it a mod I would probably be asked for options to display none, one, or both dates on posts and/or PMs and to also have independently configurable date formats ...

Guess I'll wait and see how much interest it generates ...
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

landyvlad

It is quick and simple as you say; but I think the user community would benefit in having this available as a mod for 2.1.x  Not everyone is comfortable doing manual code edits, even if they are relatively straightforward. That's my $0.02
"Put as much effort into your question as you'd expect someone to give in an answer"

Please do not PM, IM or Email me with questions on astrophysics or theology.  You will get better and faster responses by asking homeless people in the street. Thank you.

Be the person your dog thinks you are.

WolfJ

Thanks for pointing me to this topic from my other posts I made but I'm still using 2.0.19, would this still work?

Chewing on Cyanide


GL700Wing

Quote from: WolfJ on August 22, 2022, 11:40:57 AMThanks for pointing me to this topic from my other posts I made but I'm still using 2.0.19, would this still work?
@WolfJ I've confirmed that all the file edits for this tip/trick are also compatible with SMF 2.0.19 so I've updated the topic subject accordingly.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

davo88

Thanks for these edits GL700Wing, they produce a nice neat display.

Quote from: GL700WingNotes:
1. To keep the dates as compact as possible the 'Date Joined' and 'Last Active' dates are shown in the 'MMM YYYYY' format - if you want to use a different date format you will need to modify the relevant line(s) of code in ./Sources/Load.php;

I would quite like to try and squeeze in the day and time with 'Last Active' somehow. Maybe shortening 'Last Active' to just 'Last' would do it. Could you tell me the additional fields to load for those two please?

GL700Wing

Quote from: davo88 on March 14, 2024, 10:12:02 PMThanks for these edits GL700Wing, they produce a nice neat display.
You're welcome!

Quote
Quote from: GL700WingNotes:
1. To keep the dates as compact as possible the 'Date Joined' and 'Last Active' dates are shown in the 'MMM YYYYY' format - if you want to use a different date format you will need to modify the relevant line(s) of code in ./Sources/Load.php;

I would quite like to try and squeeze in the day and time with 'Last Active' somehow. Maybe shortening 'Last Active' to just 'Last' would do it. Could you tell me the additional fields to load for those two please?
You can make the field titles whatever you want by changing the text for the relevant strings (ie, 'dateJoined' and 'lastActive') in the file ./Themes/default/languages/Modifications.english.php.

In ./Sources/Load.php you'll need to change the date/time format for 'date_joined' and 'last_active' from '%b %Y' to the format you want to use - details of supported date/time formats shown below:
You cannot view this attachment.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

davo88

Great info GL700Wing - thank you. With all those time formats to choose from, I should be able to fit a day and time in there.

davo88

SMF 2.1.4
Mods
 - TinyPortal
 - Forum Width Setting
 - Quick New Topic Button
 - Board Sorting Method
 - Custom Form Mod
 - Optimus
 - Login Menu Button

I seem to be getting error messages generated by both Display.template.php and PersonalMessage.template.php from this line:
if (allowedTo('admin_forum') || !$message['member']['is_hidden'])In the log, the error is described as:
2: Undefined array key "is_hidden"
So far, it seems to be generated when a member views a topic or the list of messages in their Inbox. But the error does not occur when an admin does the same things. Any thoughts on what might be causing this?

davo88


GL700Wing

Quote from: davo88 on April 30, 2024, 10:10:34 PMI seem to be getting error messages generated by both Display.template.php and PersonalMessage.template.php from this line:
if (allowedTo('admin_forum') || !$message['member']['is_hidden'])In the log, the error is described as:
2: Undefined array key "is_hidden"
So far, it seems to be generated when a member views a topic or the list of messages in their Inbox. But the error does not occur when an admin does the same things. Any thoughts on what might be causing this?
Oops ...

Try changing both instances of:
!$message['member']['is_hidden'])To:
empty($message['member']['is_hidden'])
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

davo88

I think I did the edits properly...
// Tip/Trick: Show 'Date Joined' and 'Last Active'.
        // Maybe also show when they were last active.
        if (allowedTo('admin_forum') || empty($message['member']['is_hidden'])
            echo '
              <li>', $txt['lastActive'], $message['member']['last_active'], (!empty($message['member']['is_hidden']) ? ' (' . $txt['hidden'] . ')' : ''), '</li>';

Unfortunately, that seems to make both the display template and the PM template unable to load at all and we get a white screen with an error message..
Parse error: syntax error, unexpected token "echo" in .../Themes/default/Display.template.php on line 589Parse error: syntax error, unexpected token "echo" in .../Themes/default/PersonalMessage.template.php on line 385
So that's the 'echo' on the very next line in both cases.

GL700Wing

Apologies - trying to do this without being able to test it because I'm on vacation.

My previous edit suggestion was missing a closing ')'

Try changing both instances of:
!$message['member']['is_hidden'])To:
empty($message['member']['is_hidden']))
Note the double '))' at the end ...

Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

davo88

Good work GL700Wing. No errors so far after adding the bracket. Thanks very much for fixing this quickly and apologies for interrupting your holiday.

GL700Wing

Quote from: davo88 on May 01, 2024, 04:13:08 PMGood work GL700Wing. No errors so far after adding the bracket. Thanks very much for fixing this quickly and apologies for interrupting your holiday.
Great - thanks for letting me know - I'll update the first post in the topic with the correct code.

Also, and even though I'm on holidays I still check in from time to time just in case there is something I need to do ...
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Steve

Quote from: davo88 on May 01, 2024, 04:13:08 PMapologies for interrupting your holiday
Hard not to. Her holidays are 6 months long!

j/k with ya @GL700Wing! And yes, you do detect a note of jealousy.
DO NOT pm me for support!

Advertisement: