Hi there,
Sorry, I know Kirby 2 isn’t supported anymore and I’m working on upgrading my sites to Kirby 3, but in the meantime, I have a problem with sorting a datetime field in my templates on Kirby 2. It’s sorting fine by date but time isn’t working properly - e.g. a page with the time 1:30pm is showing up before a page with the time 11:00am on the same day…
This is my field blueprint:
when:
label: Date and Time
type: datetime
date:
format: DD-MM-YYYY
time:
format: 12
interval: 15
And this is the way I’m sorting it in my template:
->sortBy('when', 'asc')
Any suggestions?
Thanks very much,
Rach
Hmm, it seems I will need to separate these out into separate date and time fields for upgrading to Kirby 3 anyway - is that true? In which case is anyone able to suggest a script for doing this so I don’t have to update them all manually?
Thanks!
Rach
Since you can’t use a callback with sortBy in Kirby 2, I’d create a custom page method or model that convert the datetime into a Unix timestamp then sort by this value.
Hi @texnixe, I’m in the midst of upgrading this site to Kirby 3, and notice that this still isn’t outputting in the right time order - what would you suggest in a Kirby 3 context?
Thanks very much,
Rach
Ah sorry, figured this one out finally too! In Kirby 3:
->sortBy(function ($page) {
return $page->when()->toDate();
}, 'asc');