We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46886
    • 1,154 Posts
    Hello, I am on the trail of a bug and want to fix it finally. smiley

    Age in profile in the forums defaults to 44, but is properly calculated once a birthdate is provided when updating profile.

    However I am demanding the birthdate from users in registration, and it doesn't lead to a proper calculation in profile.

    BobRay gave me the info that the birthdate field is listed as "dob", and this was confirmed when I changed my registration form, when I had the field called "birthdate" it was put in as an extended field, but when it was "dob" it was not.

    My goal here is to have the age value listed properly, and alternatively, I could provide the birth year of the user, that would amount to much the same thing. I definitely need to get that birthdate at registration so I can confirm the age and level of the user, but I don't want this personal info to be available to others. And because I want it in registration, I don't want to allow users to change it later, in update profile.

    The current code is [[+age]], but of course that doesn't work properly. I have tried to display birthdate as an experiment, but I find that it doesn't seem to work. I have tried [[+dob]], [[+dob:isDate=`%m/%d/%Y^'`]], and various combinations, like [[*dob]] or [[+age]]. All with no love.

    I also find my extended values are not displayable using the typical code. For example, I created a value called hometown, and hoped to display this value in the profile using [[+hometown]]. The value is in the extended fields, but won't display...

    Here is my register code:

    <legend>Register</legend>
    [[!Register?
    ...
      dob:required:isDate=^%m/%d/%Y^,
      email:required:email`
        &placeholderPrefix=`reg.`
    ]]
     
    
    <div class="register">
        <div class="registerMessage">[[!+reg.error.message]]</div>
        <form class="form" action="[[~[[*id]]]]" method="post">
            <input type="hidden" name="nospam" value="[[!+reg.nospam]]" />
     ...
    
            <label for="dob">Birthdate
                <span class="error">[[!+reg.error.dob]]</span>
            </label>
            <input type="text" name="dob" id="dob" value="[[!+reg.dob]]" />
     
            <div class="form-buttons">
                <input type="submit" name="registerbtn" value="Register" />

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

    [ed. note: nuan88 last edited this post 12 years, 3 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The type of the dob field is integer, 0 by default. It is stored as a UNIX timestamp. So [[!+modx.user.id:userinfo=`dob`]] produces a timestamp, while [[!+modx.user.id:userinfo=`dob`:date=`%Y-%m-%d`]] produces a readable date. So you can use [[!+modx.user.id:userinfo=`dob`:date=`%Y-%m-%d`:ago]] to get my age.

      Here's a cute little PHP script for calculating age:
      <?php
      $ageTime = -596426400; // my dob timestamp
      $t = time(); // Store current time for consistency
      $age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime;
      $year = 60 * 60 * 24 * 365;
      $ageYears = $age / $year;
      
      echo 'You are ' . floor($ageYears) . ' years old.';


      BobRay had some things to say about how MODX handles timestamps by default. http://bobsguides.com/blog.html/2013/09/18/displaying-modx-date-fields/

      [ed. note: sottwell last edited this post 12 years, 3 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 46886
        • 1,154 Posts
        Ha that is wild. I would like to reduce by 20 years myself haha!

        Very useful info, but it only seems to deepen the mystery. In my testing I have been using a value of 01/01/1991, and this still returns the 44 value, and indeed when I didn't include birthdate in registration the value appeared as 44. So this is still a bug without a doubt, but it seems to be ok when the value is set by update profile.

        Looking into this with Sottwell's kind info, I find something that I didn't notice before: On registration, the birthdate is listed as 1969-12-31 (if dob is included at registration). That is true no matter what date is actually entered. If no date was entered, it would leave the birthdate field in the manager blank, but again would still show up as 44 in the profile.

        I thought I had a workaround, because my extended field of "birthdate" is properly recorded, so I tried to take the code (which works) of [[!+modx.user.id:userinfo=`dob`:date=`%Y-%m-%d`]], and change to [[!+modx.user.id:userinfo=`birthdate`:date=`%Y-%m-%d`]].

        However, this didn't work, how to change to get that extended field? Because I found I could only display the year, which is just what I want, so if this could be used for extended fields, then that would solve this problem for the most part. I also have other extended fields like hometown, and the typical code in Discuss of [[+hometown]]is not working at all. So if I could display extended fields with a modification of this code, it would allow me to do a lot of things with the profile.
        • discuss.answer
          • 28042 ☆ A M B ☆
          • 24,524 Posts
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 46886
            • 1,154 Posts
            Ah, so it looks like this should work:

            $extended = $profile->get('extended');
            echo $extended['birthdate'];
            echo $extended['hometown'];

            Will try it out soon.

            Ah, your text didn't come out at first, just the links. Yes I will try the placeholders first.

            Thank you! [ed. note: nuan88 last edited this post 12 years, 3 months ago.]
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Ok, I have a snippet that can be used as an output modifier.

              The snippet, named "extended":
              <?php
              /* extended snippet - get extended fields */
              $userid = $input;
              $user = $modx->getObject('modUser', $userid);
              $profile = $user->getOne('Profile');
              $fields = $profile->get('extended');
              
              if ( true === isset($options) ) {
                $field = $options;
                $output = $fields[$field];
              } else {
                $output = '';
              }
              return $output;


              And its usage:
              [[!+modx.user.id:extended=`test`]]
              [ed. note: sottwell last edited this post 12 years, 3 months ago.]
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                I keep getting a funny feeling that I shouldn't need to get another user object, MODX should have one available to me already. On the other hand, what if you want to use this for another user, not the current user?
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 46886
                  • 1,154 Posts
                  Wow that looks great!~ I will use that asap, thank you so much Susan!

                  I feel a little sad that the register can't take the data properly in the first place, but actually I will have a lot of extended fields, so handling birthdate in extended is not the worst thing in the world.

                  User info is so important now, gathering that data can be quite valuable. I am working on the flow of registration and confirmation now, its a great way to grab more of that data. People remain enthused for a very short time, and you got to get the info while you can haha.

                  We just found our competitors are using a 3-4 step process for registration, that might be a bit much, but its interesting. It sort of makes sense, they only want email and password on first step, but then after email confirmation they throw people into a one time process so the user can populate the various fields, basically a customized update profile over several steps.

                  Your knowledge is much deeper than mine, of course, but because this info is in extended, I don't think modx provides suitable user objects, which I think were all created a long time ago. Maybe it could or should, but I couldn't find one result searching for extended with "user objects" that was useful.

                  Thank again! This solves a huge problem for me! I will put my finalized code into the Discuss section of the forums as a guide for newbies once I finally finalize.
                    • 3749
                    • 24,544 Posts
                    Looks great. Let me just suggest a couple of sanity checks to prevent possible PHP errors or warnings in the case of a bad user ID or a user with no profile.

                    <?php
                    /* extended snippet - get extended fields */
                    $userid = $input;
                    $output = '';
                    $user = $modx->getObject('modUser', $userid);
                    if ($user) {
                       $profile = $user->getOne('Profile');
                       if ($profile) {
                           $fields = $profile->get('extended');
                           if ( true === isset($options) ) {
                               $output = $fields[$options];
                           }
                       }
                    }
                    return $output;


                      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
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Yes, I did some cleaning up on it myself.
                      <?php
                      /**
                      /* extended snippet
                      * returns extended user field
                      * 6/2014 sottwell
                      *
                      * properties
                      * field - the extended field 
                      *
                      * Usage:
                      * [[!+modx.user.id:extended=`test`]]
                      **/
                      
                      $output = '';
                      if($input) {
                        $userid = $input;
                        $user = $modx->getObject('modUser', $userid);
                        $profile = $user->getOne('Profile');
                        $fields = $profile->get('extended');
                      
                        if ( true === isset($options) ) {
                          $field = $options;
                          $output = isset($fields[$field]) ? $fields[$field] : '';
                        }
                      }
                      return $output;
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org