News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

How to obtain number of years registered?(Anniversary icon in postbit)

Started by Bayle, October 22, 2007, 03:21:53 PM

Previous topic - Next topic

Bayle

I'm trying to add a slight modification that displays (in each post) a different gif image depending on the number of years the member has been registered.
I tried adding the following code, only to have it spit out a zero instead of the number I was expecting (I tested it on a post made by a member that joined in 2003)
          //Showing years joined
          $mytimediff = ($context['current_time']-$message['member']['registered']);
          if(!$message['member']['is_guest'])
          echo ' <br />', $mytimediff;

What is going wrong here? Any help would be greatly appreciated.

jamesk

Not really a PHP guy, but where are you getting those variables ($context['current_time'] and $message['member']['registered']) from?

Even if they were both the same TYPE (although the $message['member']['registered'] looks like a string), you'd still have to probably do some numbers manipulating...

I'm not sure how the $message['member']['registered'] variable prints, but you might try something like:

$today = getdate();
$thisYear = $today['year'];

$yearsRegistered = $thisYear - SomehowParse($message['member']['registered']);

or something like that...

Kays

Try using "$message['member']['registered_timestamp']" instead.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Bayle

Thanks for the help. Evidently the variables I was trying to use were already parsed by date() and were not usable as I needed them. The following code spits out the number of years (dropping off the modulus) the member has been registered. Now I just have to add the conditionals to make it show an image (different ones for each year)
          //Showing years joined
          $mytimenow = getdate();
          $mytimediff = intval(($mytimenow[0]-$message['member']['registered_timestamp'])/31526000);
          if(!$message['member']['is_guest'])
          echo ' <br />', $mytimediff;



Quote from: jamesk on October 22, 2007, 03:37:53 PM
Not really a PHP guy, but where are you getting those variables ($context['current_time'] and $message['member']['registered']) from?

Even if they were both the same TYPE (although the $message['member']['registered'] looks like a string), you'd still have to probably do some numbers manipulating...
I obtained them by getting a listing of the contents of the $context and $message variables:
echo '<pre>', print_r($context, true), '</pre>';
echo '<pre>', print_r($message, true), '</pre>';

Kays

#4
$message['member']['registered_timestamp'] is a Unix timestamp so you could use time() instead. Something like:

$years = floor((time() - $message['member']['registered_timestamp'])/31526000)

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Bayle

Thanks again. I'm relatively new to PHP in general, so I learn with every new modification I make. This was my latest code, which covers up to 6 years:
          //Showing years joined
          $mytimediff = floor((time() - $message['member']['registered_timestamp'])/31526000);
          if(!$message['member']['is_guest'])
          {
           if($mytimediff==1)
           echo '<br /><img src="', $settings['images_url'] ,'/1.gif" alt="Registered 1 year" />';
           if($mytimediff==2)
           echo '<br /><img src="', $settings['images_url'] ,'/2.gif" alt="Registered 2 years" />';
           if($mytimediff==3)
           echo '<br /><img src="', $settings['images_url'] ,'/3.gif" alt="Registered 3 years" />';
           if($mytimediff==4)
           echo '<br /><img src="', $settings['images_url'] ,'/4.gif" alt="Registered 4 years" />';
           if($mytimediff==5)
           echo '<br /><img src="', $settings['images_url'] ,'/5.gif" alt="Registered 5 years" />';
           if($mytimediff==6)
           echo '<br /><img src="', $settings['images_url'] ,'/6.gif" alt="Registered 6 years" />';
          }

jamesk

I'm not a PHP guy either but, how about this:

          //Showing years joined
          $maxAllowed = 6;    // or whatever
          $mytimediff = floor((time() - $message['member']['registered_timestamp'])/31526000);
          if(!$message['member']['is_guest']) && ($mytimediff >1 && $mytimediff < $maxAllowed)
          {
               echo '<br /><img src="', $settings['images_url'].$mytimediff ,'.gif" alt="Registered', $mytimediff,' year(s)" />';
           }


There might be some wrong placement of commas and quotes, but you get the logic...  The year(s) can be modified further with a check of >1 if you want to be that specific.

Also, I'm not sure what kind of images you're using for the numbers, but if it's simple like a button or something, you can probably modify it so that any number of years shows within the button -- maybe through CSS and a generic background image...

Just something to think about...

Chriss Cohn

Why not make a mod of it guys? Im sure others besides me will be pretty interested in a mod like this...

Regards, Christian

Advertisement: