Need help with How long user have been member

Discussion forum for Extension Writers regarding Extension Development.
User avatar
Stoker 4.0
Registered User
Posts: 1504
Joined: Sun Feb 13, 2011 1:33 pm
Location: Funen, Denmark
Name: Ulrik Christensen

Need help with How long user have been member

Post by Stoker 4.0 »

Hello,

Hope I can get help with a little piece of code I found a long time ago.
I use it to display how long a user have been member.

But it does not work properly.

The code:

Code: Select all

// how long a member for
		$member_for = '';
		if ($this->user->data['is_registered'] && !$this->user->data['is_bot'])
		{
			$member_length = (int) time() - (int) $this->user->data['user_regdate'];
			$years = $months = $days = 0;
			$member_for = '';
			if ($member_length)
			{    
				if ($member_length >= 31536000)
				{
					$years = (int) ($member_length / 31536000);
					$member_length = (int) $member_length - ( (int) $years * 31536000);
					$member_for .= $years > 1 ? ((int) $years . ' ' . $this->user->lang['WELCOME_YEARS'] . ', ') : ((int) $years . ' ' . $this->user->lang['WELCOME_YEAR'] . ', ');
				}
				$months = (int) ($member_length / 2628000);
				if ($months)
				{
					$member_length = (int) $member_length - ((int) $months * 2628000);
					$months = (int) $months > 1 ? ((int) $months . ' ' . $this->user->lang['WELCOME_MONTHS'] . ', ') : ((int) $months . ' ' . $this->user->lang['WELCOME_MONTH'] . ', ');
					$member_for .= $months;
				}
				$days = (int) ($member_length / 86400);
				if ($days)
				{
					$member_length = (int) $member_length - ((int) $days * 86400);
					$days = (int) $days > 1 ? ((int) $days . ' ' . $this->user->lang['WELCOME_DAYS']) : ($days . ' ' . $this->user->lang['WELCOME_DAY']);
					$member_for .= $days;
				}        
			}
		}
Gives this output
You joined: 12 May 2008, 23:26
You have been member for: 16 Years, 1 Month, 1 Day
or from another profile
You joined: 08 Mar 2010, 17:42
You have been member for: 14 Years, 3 Months, 5 Days

I do not have the knowledge to fix it, so I hope one of you can do it
Regards Stoker
User avatar
ssl
Registered User
Posts: 1988
Joined: Sat Feb 08, 2020 2:15 pm
Location: Le Lude, Pays de la Loire - France
Name: Fred Rimbert

Re: Need help with How long user have been member

Post by ssl »

If this information should be displayed in member profiles in messages, an extension exists:https://www.phpbb.com/customise/db/exte ... _timespan/
Sorry for my English ... I do my best! :anger_right:

:point_right_tone3: phpBB: 3.3.13 | PHP: 8.3.9
:point_right_tone4: [Kill spam on phpBB] - [Some French translation of extensions]
"Mistress, Mistress someone is bothering me in pm"
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 544
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: Need help with How long user have been member

Post by danieltj »

What's wrong with the code, do you mean that the time it's stating a user has been a member for is incorrect in some way?
Awesome Payments now available | Auto Moderator coming soon
User avatar
Stoker 4.0
Registered User
Posts: 1504
Joined: Sun Feb 13, 2011 1:33 pm
Location: Funen, Denmark
Name: Ulrik Christensen

Re: Need help with How long user have been member

Post by Stoker 4.0 »

danieltj wrote: Sun Jun 09, 2024 3:37 pm What's wrong with the code, do you mean that the time it's stating a user has been a member for is incorrect in some way?
According to the output it is off:
You joined: 12 May 2008, 23:26
You have been member for: 16 Years, 1 Month, 1 Day
or from another profile
You joined: 08 Mar 2010, 17:42
You have been member for: 14 Years, 3 Months, 5 Days
Regards Stoker
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53551
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}

Re: Need help with How long user have been member

Post by Brf »

Your code is counting months in seconds. it is assuming every month is 30.4 days long. It is also assuming every year is 365 days. To do it right you need to convert the timestamps into dates, with year-month-day and subtract the individual pieces.
User avatar
Stoker 4.0
Registered User
Posts: 1504
Joined: Sun Feb 13, 2011 1:33 pm
Location: Funen, Denmark
Name: Ulrik Christensen

Re: Need help with How long user have been member

Post by Stoker 4.0 »

Thanks, I will take a look at an existing extension that has a better count
Regards Stoker

Return to “Extension Writers Discussion”