user500665
user500665

Reputation: 1362

Get dates between start and end in Fluid

In a fluid template I have a start date and an end date. How do I get the dates in between so I have a list of all dates?

<f:format.date format="%d">{newsItem.datetime}</f:format.date>
<f:format.date format="%d">{newsItem.eventEnd}</f:format.date>

Upvotes: 1

Views: 849

Answers (2)

Ravi Sachaniya
Ravi Sachaniya

Reputation: 1633

For this you can create your own ViewHelper and in that you need to pass start and end dates and it will return DatePeriod object which you can iterate using for loop (<f:for>) in Fluid template.

You can try something like :

Use ViewHelper in Fluid Template as :

{namespace vh=Vendor\ExtensionKey\ViewHelpers}

<f:for each="{vh:DateRange(startdate:'{starttime}', enddate:'{endtime}')}" as="dates">
    <f:format.date format="%d.%m.%Y">{dates}</f:format.date> <br/>
</f:for>

ViewHelper Class :

<?php
namespace Vendor\ExtensionKey\ViewHelpers;

/**
* Date Range ViewHelper
*/
class DateRangeViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
    /**
     * @return void
     */
    public function initializeArguments()
    {
        parent::initializeArguments();
        $this->registerArgument('startdate', 'string', 'start date', true);
        $this->registerArgument('enddate', 'string', 'end date', true);
    }

    /**
     * @return \DatePeriod $dateRange
     */
    public function render()
    {
        $startdate = new \DateTime($this->arguments['startdate']);
        $enddate = new \DateTime($this->arguments['enddate']);

        $interval = new \DateInterval('P1D'); // 1 Day
        $dateRange = new \DatePeriod($startdate, $interval, $enddate);

        return $dateRange;
    }
}

References :

Hope this help you!

Upvotes: 1

Mathias Brodala
Mathias Brodala

Reputation: 6460

There is no out of the box solution for this but you can write your own viewhelper which takes these two DateTime objects and returns a DatePeriod object which you can conveniently iterate with <f:for> in your template. For every iteration you get a DateTime object again which you can format as usual.

Upvotes: 1

Related Questions