Error showing last login ago

I’m trying to show how long time ago the user was logged in, for some reason doesn’t work.

The date is right when I print but not when I add ‘ago’ to the tag

This shows the date and it’s right:
[[+lastlogin:date=%d-%m-%Y %H-%M]] -> 13-02-2020 08-40

this is what it shows when I put ‘ago’ to the tag
[[+lastlogin:date=%d-%m-%Y %H-%M:ago]] -> 50 years, 1 month ago
[[+lastlogin:ago]] -> 50 years, 1 month ago

I tried in a different way but also doesn’t work, this looks like is showing the actual time
[[+lastlogin:date=%d-%m-%Y:ago]] -> 9 hours, 31 minutes ago

This is what actual time print shows.
[[!+nowdate:default=now:strtotime:date=%d-%m-%Y %H-%M]] -> 13-02-2020 09-31

Thanks in advanced.

Have you tried calling it uncached, with an ! in front, like your working example?

I just did it but I get the same

Okay, apparently the correct way to call the tag is [[+lastlogin:date:ago]] that gave me the right format but not the right information.

What I see for the ‘web users’ is in the database the column ‘thislogin’ and ‘lastlogin’ have the same value and this value is always the current session date not the last session date.

For ‘manager users’ (admin) this works perfectly but what I want is implement this only for the web users. This is my login call.

[[!+modx.user.id:is=`0`:then=`
[[!Login? 
&loginTpl=`CustomLoginTpl` 
&logoutTpl=`CustomlgnLogoutTpl` 
&contexts=`web,mgr`	
&errTpl=`lgnErrTpl`
&redirectToPrior=`1`
&logoutResourceId=`7`
&loginResourceId=`4`
]]

:else=
`]]

It sounds like the when the web users log in, the lastlogin value gets updated to the current time during the login process. Is the lastlogin value correct before the user logs in?

You might be able to capture the lastlogin value before it gets changed with a snippet that runs on the log in page before the user logs in, or (maybe easier) with a plugin attached to one of the early login events.

1 Like

Thank you Bobray, I did what you told me and I think this it’s working well.
This is my plugin

 switch ($modx->event->name) {
     case 'OnBeforeWebLogin':    
        $object = $modx->getObject('modUser', array('username' => $username));
        $profile =  $object->getOne('Profile');
        $last = $profile->get('thislogin');
        $_SESSION['LastLogin'] = $last;       

     case 'OnWebLogin':        
        $iduser = $user->get('id');
        $last =  $_SESSION['LastLogin'];
        $c = $modx->newQuery('modUserProfile');
        $c->where(array('internalKey' => $iduser));
        $profile = $modx->getObject('modUserProfile', $c);
        $profile->set('lastlogin', $last);
        $profile->save();     
}

Looks perfect! :smiley:

What’s your tag to display the value?

BTW, I think you could set a placeholder in the plugin.

I’m saving the right lastlogin value in the OnWebLogin event, and I’m getting it using [[+lastlogin:date:ago]] in the chunk.

Thanks. I’ll probably do a blog post on it eventually.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.