On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hello, I want to add in a custom value for date of registration for each user. It seems rather simple, but I am not sure how to proceed.

    Does modx keep track of time? If so, it could be relatively easy.

    Any advice on adding this value to registration would be appreciated.

    This question has been answered by whitebyte. See the first response.

    • discuss.answer
      If you are using Login extra (Login.Register snippet) for user registration, just create new snippet with the following code:
      <?php
      $profile = $hook->getValue('register.profile');
      $extended = $profile->get('extended');
      $extended['registerdate'] = date('d-m-y');
      $profile->set('extended', $extended);
      
      if ($profile->save()) {
          return true;    
      } else {
          $modx->log(modX::LOG_LEVEL_ERROR, '[setRegisterDate]: failed to save user profile!');
          return false;
      }

      , and add it to your &postHooks parameter in Register snippet call. This is copy-paste from existing project, so I'm pretty sure that this code works.
      • Thank you whitebyte. I am finally getting closer to my goal of having and displaying the register date of users.
        • @whitebyte - Do you mind if I create a blog post on this (with credit to you, of course)?

          FWIW, in some situations, I would do this instead:

          $extended['registerdate'] = time();


          That will store a Unix timestamp in the field, which will contain more precise information. Your method is best if you always want to display the date in that particular format, but the timestamp will allow many different formats on output (with the :strftime modifier), and will also be handy if you decide you need sorting (e.g., listing the most recent registrants) or other date arithmetic down the road (e.g., "You've been a member for n years, n months, and n days" or giving special rights to long-time members). It also saves you from having to do the double conversion of strtotime() then strftime() in some cases.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • BobRay, sure, I'm glad to see that it is useful.
            $extended['registerdate'] = time();

            - good catch!
            • Just want to follow up and say that this worked like a charm! I put the posthook reference right before the useExtended.

              Thank you whitebyte! That's 18 kinds of awesome!

              Then, I looked at Bob's comment, and decided to get this number in there too, even though i won't use it right away. So, I just made a new snippet:

              <?php
              $profile = $hook->getValue('register.profile');
              $extended = $profile->get('extended');
              $extended['registertimestamp'] = time();
              $profile->set('extended', $extended);
               
              if ($profile->save()) {
                  return true;    
              } else {
                  $modx->log(modX::LOG_LEVEL_ERROR, '[setRegisterDate]: failed to save user profile!');
                  return false;
              }


              Which gives me a new extended field with a number: 1425198000, which I assume is a valid unix time.

              Now I got both values! Too cool!
              • @nuan: Very clever and forward thinking.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting