We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Fresh out of the Model! Markdown & Time Offset

I like having an easy way to format items straight out of the database, this is the best way I've found.. I know I shared something similar to this already, but I added a markdown item and it's pretty sweet! Note, you might want to make the markdown instantiation a singleton within the BaseModel.

I have a getOffset and markdown: Im using the composer package: "erusev/parsedown": "0.9.4",

Controller

    // Just grabbing a result from any model 
    $this->view->setVars([
        'threads' => \ProductThread::find([
            'category_id' => 1
            'order'=> 'id DESC'
        ]),
    ]);

View

    Calling my custom BaseModel items
    {{ thread.getOffset('created_at') }}
    {{ thread.markdown('content') }}
    {{ reply.markdown('content') }}

BaseModel

    <?php

    class BaseModel extends \Phalcon\Mvc\Model
    {

        public function onConstruct()
        {
            $this->di = \Phalcon\DI\FactoryDefault::getDefault();
        }

        /**
         * Get the date offset
         *
         * @param  mixed  $field (Optional: created_at, updated_at, etc)
         *                       uses now by default
         *
         * @return string
         */
        function getOffset($field = false)
        {
            $use = 'now';
            if ($field && property_exists($this, $field)) {
                $use = $this->{$field};
            }

            $session = $this->di->getSession();
            $timezone = strtolower($session->get('timezone'));

            $time = strtotime($use);

            $offset = 0;
            if ($timezone && $timezone != 'utc')
            {
                $userDateTime = new DateTime($use, new \DateTimeZone($timezone));
                $offset = $userDateTime->getOffset();
            }

            $userTime = (int) ($time + $offset);
            return date('F jS, Y h:ia', $userTime);
        }

        // --------------------------------------------------------------

        /**
         * Parses markdown for any given field
         *
         * @param   string  $field
         * @return  markdown
         */
        function markdown($field)
        {
            if ($field && property_exists($this, $field)) {
                $use = $this->{$field};
            }

            $parsedown = new \Parsedown();
            return $parsedown->parse($use);
        }

        // --------------------------------------------------------------

    }

public/index.php

      // --------------------------------------------------------------------
      // Load Composer
      // --------------------------------------------------------------------
      $autoload_file = "../vendor/autoload.php";
      if (!file_exists($autoload_file)) {
          throw new \Exception('$ composer install');
      }

I hope somebody finds this handy or dandy!

Thank you for sharing!