Jump to content
creativejay

Using concrete dates instead of relative ones in admin

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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);
});

 

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

Just one thing to consider - it may result in some weirdness when both actual and relative versions are shown together, eg:

image.png.3d7bcee640da8bcbc45730a93bbdfa53.png

  • Like 3

Share this post


Link to post
Share on other sites
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);
	});
}

 

  • Like 2

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...