statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.05k stars 530 forks source link

Sorting collection by date gives wrong order #10372

Closed MrMooky closed 4 months ago

MrMooky commented 4 months ago

Bug description

I'm trying to get the latest posts from a collection like this:

{{ collection:posts sort="date:desc" limit="{limit}" as="posts" }}.

I was wondering why the outcome was not the expected one and saw that the date did not store the hour, minute, second (see screenshot). It's kind of obvious, but when using date|desc for sorting, I'd expect the sorting to be accurate. I may be missing another way to accomplish this and this could not be considered a bug.

If you create 2 entries 5 minutes apart and I use the above mentioned collection query, I expect that the latest article shows up first. But due to the date, the sorting is not right.

Screenshot_2024-06-27_at_19 40 01

How to reproduce

Create a couple of entries in a collection and use the following query to retrieve them:

{{ collection:posts sort="date:desc" limit="{limit}" as="posts" }}

The latest article will likely not be the first in the list.

Logs

No response

Environment

Environment
Application Name: XXXXX
Laravel Version: 11.13.0
PHP Version: 8.2.20
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: XXXXX.test
Maintenance Mode: OFF
Timezone: UTC
Locale: de

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: log
Queue: sync
Session: file

Livewire
Livewire: v3.5.1

Statamic
Addons: 3
Sites: 1
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.11.0 PRO

Statamic Addons
jonassiewertsen/statamic-livewire: 3.6.0
mitydigital/sitemapamic: 3.0.2
stillat/antlers-components: 2.2.0

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

jasonvarga commented 4 months ago

Edit the date field in your blueprint and make sure you enable time. Otherwise, only date will get saved. You can see that the times in all the carbon instances show them as midnight.

MrMooky commented 4 months ago

Yes, that will work. The reason I created an issue for that was because editors have to set a time now. Maybe for a future update you guys could consider how Laravel does it. When an entry is published, the date will get the current date with the current time. Sort of like the created_at column for Laravel models. And then editors can optionally change that, which will likely not happen.

Anyway, just a thought.