Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Robin

Time format as time since? (e.g. 3 days ago)

It is possible to set a time format as time since?

So I could have last modified column as “24 minutes ago” for example.

8 years, 7 months ago
Stefan van den Dungen Gronovius
Developer

Hi Robin,

This is not possible with the Admin Columns user interface, but your are able to manage this with some of our filters.
You can have a look at this page: https://www.admincolumns.com/documentation/filter-reference/cac-column-value/

The function below gives you a base setup that shows you the amount of days from now for alle custom fields set to date. You may want an extra check if you want to trigger a specific column.

function cac_datefield_days_from_now_value( $value, $object_id, $column ) {
	if ( 'date' == $column->get_option( 'field_type' ) ) {
		$now = time();
		$date = strtotime( $value );
		$seconds = $now - $date;
		$minutes = $seconds / 60;
		$hours = $seconds / 3600;
		$days = $seconds / 86400;

		if( $days > 0 ){
			$value = ceil( $days ) . ' days ago';
		} else {

			$value = (ceil( $days ) * -1 ) + 1 . ' days ahead';
		}

	}

	return $value;
}

add_filter( 'cac/column/value', 'cac_datefield_days_from_now_value', 10, 3 ); // All columns for all content types
8 years, 7 months ago
Tobias Schutter
Developer

There is also a WordPress function you could use ‘human_time_diff‘ to make the code a bit shorter.

8 years, 7 months ago
Stefan van den Dungen Gronovius
Developer

@Tobias
Nice, didn’t know that one

8 years, 7 months ago
Robin

What would be an example of the above code using the human_time_diff function? Forgive my lack of coding skills.

8 years, 7 months ago
Robin

Also targeting just the “Last Modified” column type.

8 years, 7 months ago
Tobias Schutter
Developer

This will display the time difference using human_time_diff for the “last modified” column.


function my_cac_display_human_time_diff( $value, $object_id, $column ) {
	if ( 'column-modified' === $column->get_type() ) {
		$value = human_time_diff( strtotime( $value ), time() ) . ' ago';
	}

	return $value;
}

add_filter( 'cac/column/value', 'my_cac_display_human_time_diff', 10, 3 );
8 years, 7 months ago

You must be logged in to reply to this topic.