We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41918
    • 1 Posts
    Hi guys, here are many of similar questions but no one of them seems to have an answer.

    I'm trying to assign default value for Date template variable using @EVAL binding. It should be set to current date.

    I tried to use this line:

    @EVAL return date('Y-m-d H:i:s');


    and this one:

    @EVAL return time();


    but had no luck and default value is displayed as 1970-01-01 12:00 am.

    Am I doing something wrong or such bindings just doesn't working with Date TVs?

    Thanks!

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

      • 39333
      • 151 Posts
      I can't answer your question about @eval but if you want a date tv to show the current date by default all you have to do is go to the tv's input options and type the word "today", no quotes, in the default value text box.

      You can format the output in the output tab and put in whatever you want for your a typical date format.

      i.e. %a %b %e, %Y for Sun Nov 18, 2012
        MODX...the Zen of CMS
        "Bight off more than you can chew and keep right on chewing."
        • 38878
        • 255 Posts
        I am also in need of this. Is there any way to set the default value of a TV to the date time it was saved? I have a migx tv for which I need to time stamp it's creation for use in a resource. Is eval binding not an acceptable as a default TV input?
          • 38878
          • 255 Posts
          I am also in need of this. Is there any way to set the default value of a TV to the date time it was saved? I have a migx tv for which I need to time stamp it's creation for use in a resource. Is eval binding not an acceptable as a default TV input?
            • 3749
            • 24,544 Posts
            If you use @EVAL, you'll get a new date every time the default value is used.

            I think it would take a plugin to do what you want, but a very simple one -- connected to OnTVFormSave.

            This would put a unix timestamp for the current date/time in the TV:

            $tv->set('default_text', time();
            $tv->save();


            It won't change when a value is set for a resource, but it will change if you edit and save the TV itself. To prevent that, put this at the top:

            if ($mode != modSystemEvent::MODE_NEW) {
                return '';
            }


            If you need a human-readable date/time in the default value, use something like this:

            $tv->set('default_text', strftime("%Y-%m-%d %H:%M",time());



              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
              • 38878
              • 255 Posts
              Thanks! Ill give that a try.

              Quote from: BobRay at Feb 13, 2013, 06:13 AM
              If you use @EVAL, you'll get a new date every time the default value is used.

              I think it would take a plugin to do what you want, but a very simple one -- connected to OnTVFormSave.

              This would put a unix timestamp for the current date/time in the TV:

              $tv->set('default_text', time();
              $tv->save();


              It won't change when a value is set for a resource, but it will change if you edit and save the TV itself. To prevent that, put this at the top:

              if ($mode != modSystemEvent::MODE_NEW) {
                  return '';
              }


              If you need a human-readable date/time in the default value, use something like this:

              $tv->set('default_text', strftime("%Y-%m-%d %H:%M",time());



                • 40045
                • 534 Posts
                Quote from: eric.swd at Nov 18, 2012, 04:01 PM
                I can't answer your question about @eval but if you want a date tv to show the current date by default all you have to do is go to the tv's input options and type the word "today", no quotes, in the default value text box.

                You can format the output in the output tab and put in whatever you want for your a typical date format.

                i.e. %a %b %e, %Y for Sun Nov 18, 2012

                perfect, exactly what I needed =), any ideas how to control the time that is shown? it always shows 12:00am...no idea why =D

                EDIT: Nevermind, just use the word "now" (no quotes) instead of "today"...what other keywords can we use, can't find anything in the docs...
                  • 39333
                  • 151 Posts
                  Good question smiley I actually didn't find that anywhere was just doing some trial and error testing and stumbled on it. Was an AHA moment! Went out and celebrated with an ice cream laugh

                  Good to know there's other short codes too.
                    MODX...the Zen of CMS
                    "Bight off more than you can chew and keep right on chewing."
                    • 40045
                    • 534 Posts
                    unbelievable, that such a goodie is not documented, I'll add the two we found I'll added all I found, if anybody knows 1. where to find them in the code (I tried everywhere I could, but couldn't find any hardcoded strings like "today" and "now" in files that have to do with dateTV types) or 2. knows others, please post them =D

                    EDIT: OK, found some more with further trial end error =D, what I have now is: yesterday, today, now, tomorrow and the possibility to add or subtract hours from the current time, eg. +72h (goes 72 hours back, read new rtfm for my theory why =D) or -72 (72 hours in the future)...

                    Quote from: eric.swd at Mar 24, 2013, 05:52 PM
                    Good question smiley I actually didn't find that anywhere was just doing some trial and error testing and stumbled on it. Was an AHA moment! Went out and celebrated with an ice cream laugh

                    Good to know there's other short codes too.
                    [ed. note: exside last edited this post 11 years, 1 month ago.]
                      • 42046
                      • 436 Posts
                      Quote from: exside at Mar 25, 2013, 06:12 PM
                      if anybody knows 1. where to find them in the code (I tried everywhere I could, but couldn't find any hardcoded strings like "today" and "now" in files that have to do with dateTV types)

                      Poked around and they're in /core/model/modx/filters/modoutputfilters.class.php

                      There's code for 'fuzzydates' and 'ago'. Line 456 and 474.