Using WordPress ‘media_date_column_time’ PHP filter

The media_date_column_time WordPress PHP filter allows you to modify the published time of an attachment displayed in the Media list table.

Usage

add_filter('media_date_column_time', 'my_custom_media_date_column_time', 10, 3);
function my_custom_media_date_column_time($h_time, $post, $column_name) {
    // your custom code here
    return $h_time;
}

Parameters

  • $h_time (string) – The published time of the attachment.
  • $post (WP_Post) – The attachment object.
  • $column_name (string) – The name of the column in the Media list table.

More information

See WordPress Developer Resources: media_date_column_time

Examples

Change the date format

Customize the date format of the published time.

add_filter('media_date_column_time', 'change_media_date_format', 10, 3);
function change_media_date_format($h_time, $post, $column_name) {
    if ('date' == $column_name) {
        $h_time = get_the_date('F j, Y', $post);
    }
    return $h_time;
}

Display time since published

Show the time elapsed since the attachment was published.

add_filter('media_date_column_time', 'display_time_since_published', 10, 3);
function display_time_since_published($h_time, $post, $column_name) {
    if ('date' == $column_name) {
        $h_time = human_time_diff(get_the_time('U', $post), current_time('timestamp')) . ' ago';
    }
    return $h_time;
}

Add a custom prefix

Add a custom prefix to the published time.

add_filter('media_date_column_time', 'add_custom_prefix', 10, 3);
function add_custom_prefix($h_time, $post, $column_name) {
    if ('date' == $column_name) {
        $h_time = 'Published: ' . $h_time;
    }
    return $h_time;
}

Hide the published time

Hide the published time for a specific user role.

add_filter('media_date_column_time', 'hide_published_time_for_role', 10, 3);
function hide_published_time_for_role($h_time, $post, $column_name) {
    if ('date' == $column_name) {
        $user = wp_get_current_user();
        if (in_array('subscriber', $user->roles)) {
            $h_time = '';
        }
    }
    return $h_time;
}

Highlight recently uploaded attachments

Highlight the published time of attachments uploaded within the last 24 hours.

add_filter('media_date_column_time', 'highlight_recent_attachments', 10, 3);
function highlight_recent_attachments($h_time, $post, $column_name) {
    if ('date' == $column_name) {
        $uploaded_time = get_the_time('U', $post);
        if (current_time('timestamp') - $uploaded_time < DAY_IN_SECONDS) {
            $h_time = '<strong>' . $h_time . '</strong>';
        }
    }
    return $h_time;
}