We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23085
    • 9 Posts
    I include PHP scripts by snippets and cannot use the PHP date() function.
    Example:
    $this_year = date(’Y’);
    generates the white page saying "Error while inserting event log into database."

    I didn’t get this error with any other PHP code yet.

    This is an original installation of MODx, there were entries in the event table. I emptied the table, tried all maintenance options available in PhpMyAdmin (check, repair, etc) but it didn’t help. Any hints other than the date() does not work? (Which is hopefully not the case).
      • 22303 MODX Staff
      • 10,725 Posts
      Problem inserting event log into database means that the database connection has gone away during the request and so it cannot actually log the error that is being thrown. Check your php error_log if you have it configured to log to file. Otherwise, I would imagine the problem lies with another component, as calling the date() function should definitely not cause an error like that.
        • 26903
        • 1,336 Posts
        Just to allay any fears about the date function I’ve put the code above in a snippet and called it on a test page, works fine.
          Use MODx, or the cat gets it!
          • 23085
          • 9 Posts
          Thanks, I guess there is something wrong with this server. It’s a development machine and apart from the PHP date() function the friendly URLs suddenly stopped to work too. Fortunately the live site is running fine and there is of course no date() problem. Thanks for the replies and for testing. We hope copying the whole MODx installation from the production site will bring the development site back to normal.

            • 1863
            • 8 Posts
            It’s a common problem, actually. Check event log in your administration area - there’s more detailed information on this error there.

            You have to set your timezone manually for some server configurations, otherwise modx stops the snippet from execution.
            Add the following line to the beginning of your snippet:

            date_default_timezone_set(’Europe/Moscow’);

            Where ’Europe/Moscow’ is your timezone.
              • 26903
              • 1,336 Posts
              You have to set your timezone manually for some server configurations, otherwise modx stops the snippet from execution.
              Er, I think not, timezone is/should be set at the server level, not in MODx, if anything stops the date() function working it would be PHP, not MODx
                Use MODx, or the cat gets it!
                • 1863
                • 8 Posts
                Quote from: shamblett at Mar 22, 2009, 02:27 PM

                Er, I think not, timezone is/should be set at the server level, not in MODx, if anything stops the date() function working it would be PHP, not MODx

                No, this is exactly the problem, and MODX event log area specifies in case of this error, that date() function requires implementing date_default_timezone_set usage in some cases. Including situations, when you are unable adjust server-side timezone settings. Otherwise using date() is considered "unsafe" by MODX.

                It’s really backward, because you get the ’problem inserting event log into database’ error, which is seemingly unrelated to the problem. It took me quite a lot of digging to sort this out.

                Once again - that’s what solved it for me. Set the timezone in the beginning of the snippet.



                  • 26903
                  • 1,336 Posts
                  Fair enough, I’m not surprised this took you a lot of digging, it does seem fairly odd, I must have always been on servers where date() just worked I guess.
                    Use MODx, or the cat gets it!
                    • 4310
                    • 2,310 Posts
                    I’ve seen a module cause an error to be logged about it being unsafe to use date().
                    Can’t remember which module or site it was though.
                      • 22303 MODX Staff
                      • 10,725 Posts
                      If you are getting this error, your server does not have a date.timezone configuration value set. See http://php.net/manual/en/function.date-default-timezone-get.php for precedence and additional information.