I wonder what’s a good way to do that. I have a collection which has mixed date formatting and I want to sort them by date, however they are not in the correct order, and i wondered how i could just sort them by unix timestamp. such as first converting the date field, then just that sort ASC
I already tried several attempts in doing so but it failed and was kinda messy, so maybe there’s a function i didn’t think of by now and i am happy to hear!
is this possible w/o updating content but just modifying output?
Yes, you don’t have to update the content but can use it as kind of virtual field.
function changeUnix($page) {
if($page->unix()->empty()){
try{
$page->update(array(
'unix' => strtotime($page->datum())
));
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
map() helped me, instead of fiddeling with virtual stuff, i chose to just rewrite a value when the field is not existent already, guess that should do. the sorting is working now.
Here a version without creating a new field:
<?php
$collection = page('somepage')->children()->visible()->map(function($page) {
$page->timestampfield = strtotime($page->date());
return $page;
})->sortBy('timestampfield', 'desc');