creativejay Posted January 4, 2021 Posted January 4, 2021 In things like PageLister and Form entries, date fields are displayed as relative (2 seconds ago, 22 hours ago, 3 days ago). Is there an easy way (a universal setting) to toggle these to display the actual date and time?
Robin S Posted January 4, 2021 Posted January 4, 2021 You can use a hook to replace all relative time strings in admin with a specific date format. Add the following to /site/templates/admin.php, at the top after the namespace declaration: $wire->addHookBefore('WireDateTime::relativeTimeStr', function(HookEvent $event) { $timestamp = $event->arguments(0); // If given a date string, convert it to a timestamp if(!ctype_digit("$timestamp")) $timestamp = strtotime($timestamp); $event->replace = true; // Set your preferred date format below $event->return = date('Y-m-d H:i:s', $timestamp); }); 3 1
adrian Posted January 5, 2021 Posted January 5, 2021 Just one thing to consider - it may result in some weirdness when both actual and relative versions are shown together, eg: 3
creativejay Posted January 5, 2021 Author Posted January 5, 2021 Perfect, thank you Robin! And thanks for the bit to consider, adrian! 1
Robin S Posted January 5, 2021 Posted January 5, 2021 15 hours ago, adrian said: Just one thing to consider - it may result in some weirdness when both actual and relative versions are shown together Depending on how much mixing of relative and absolute dates occur on a single page, it might be possible to avoid those scenarios by excluding specific processes and/or URL segments, e.g. if($page->process != 'ProcessPageEdit') { $wire->addHookBefore('WireDateTime::relativeTimeStr', function(HookEvent $event) { $timestamp = $event->arguments(0); // If given a date string, convert it to a timestamp if(!ctype_digit("$timestamp")) $timestamp = strtotime($timestamp); $event->replace = true; // Set your preferred date format below $event->return = date('Y-m-d H:i:s', $timestamp); }); } 2
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now