Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Date Range Method

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1769 Views
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Date Range Method Link to this post

    I've found the $Date.Range(date2) method at http://doc.silverstripe.com/doku.php?id=date. Trying to use it I came across the same errors as post in the archive at http://silverstripe.org/archive/show/99156#post99156. As it seems I'm not the only one experiencing the issue I thought I would post the error again, and my solution.

    When trying to call $Date.RangeString($EndDate) from a template I saw the error:

    Parse error: syntax error, unexpected T_STRING, expecting ')' in blog.templates.Layout.BlogHolder.ss on line 99

    Which I understand is a result of how the templates work.

    Tried

            $this->Date->RangeString($this->EndDate);

    next which resulted in the error:

    Fatal error: Call to a member function RangeString() on a non-object in \blog\code\BlogEntry.php on line 188

    The final solution that worked for me was (BlogEntry.php):

       
    // To create a complete example I've included the EndDate db entry:
    static $db = array(
       "Date" => "SSDatetime",
       "EndDate" => "SSDatetime",
    }

    /**
    * Outputs date span or single date if no end date defined
    */
    function DateRange() {
        $start = new Date();
        $start->setValue($this->Date);

        if (!is_null($this->EndDate)) { // Check if there is an end date
           $end = new Date();
           $end->setValue($this->EndDate);
           return $start->RangeString($end);
        } else {
           return $start->Long();
        }
    }

    For beginners I'm using $DateRange in the templates (e.g. BlogSummary.ss) to output the daterange.

  • JackAttack
    Avatar
    Community Member
    12 Posts

    Re: Date Range Method Link to this post

    Great solution, thanks!

    1769 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.