Call to a member function toDate() on string

Quick question:

<ul>
    <?php $days = $page->children()->sortBy("date", "asc")->pluck("date", ",", true); ?>
    <?php foreach ($days as $day): ?>
        <li><?= $day->toDate("d-m-Y") ?></li>
    <?php endforeach; ?>
</ul>

Why is pluck() and toDate() not working together? If I remove the pluck() part and add ->date() behind the $day variable, I get a list with all dates, but I want to remove the doubles.

$days gives you a flat array of date strings, so $date is a string. toDate(), however, is a field method that requires a field object.

1 Like

Ah okay, thank you.

And how do I get to my solution, where all the dates are taken from all subpages and the duplicates are deleted? + be able to use the toDate() function at the end?

You cannot use this method, unless you create a new field object.

Use

<?= date('d-m-Y', strtotime($day)) ?>

instead.

1 Like

Thank you :pray: