We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    How would one go about inserting the current timestamp into a setting (system, context, group, user, etc) so that, for example, [[++created_time]] would produce the timestamp when that setting was created?
      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
      • 3749
      • 24,544 Posts
      Try this:

      $setting = $modx->getObject('modSystemSetting', array('key' => 'SettingKey'));
      $setting->set('value', time());


      If it's a date setting, it will be converted to human-readable form when retrieved, but usually not in the format you want. I would be inclined to make it an integer or text field and use the :strftime output modifier in the setting tag. If it's a date field, you need to do :strtotime:strftime, which is kind of ridiculous.

        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
        I know how to do it in code. I was referring to using the user management GUI. I know for element properties and Media Sources you can use MODX tags, and there are some variables you can use in settings, like {base_url}, I was just wondering if anyone knew of something like that to get a current timestamp, or even the current date.
          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
          • 3749
          • 24,544 Posts
          That's a tough one. Tags in settings are interpreted on the way out, not on the way in (if at all), so even if you could do it, you'd be getting the time the setting was retrieved. There are no events fired when saving a setting.

          The only way I can think of would be a plugin attached to OnUserFormSave, using a version of the code above. Something like this:

          $settingKey = 'MySetting';
          
          $setting = $modx->getObject('modSystemSetting', array('key' => $settingKey));
          if (!$setting) {
             $setting = $modx->newObject('modSystemSetting');
             $setting->set('key', $settingKey);
             $setting->set('value', time());
             $setting->save();
          } else {
              if (empty($setting->get($value)) {
                  $setting->set('value', time());
                  setting->save();
              }
          }
          return '';
          



            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