Using WordPress ‘get_post_modified_time’ PHP filter

The get_post_modified_time WordPress PHP filter allows you to modify the localized time a post was last modified.

Usage

add_filter('get_post_modified_time', 'your_custom_function', 10, 3);
function your_custom_function($time, $format, $gmt) {
  // your custom code here
  return $time;
}

Parameters

  • $time (string|int) – Formatted date string or Unix timestamp if $format is ‘U’ or ‘G’.
  • $format (string) – Format to use for retrieving the time the post was modified. Accepts ‘G’, ‘U’, or PHP date format. Default ‘U’.
  • $gmt (bool) – Whether to retrieve the GMT time. Default false.

More information

See WordPress Developer Resources: get_post_modified_time

Examples

Add a custom timezone

Adjust the post modified time to a custom timezone.

add_filter('get_post_modified_time', 'adjust_modified_time_to_custom_timezone', 10, 3);
function adjust_modified_time_to_custom_timezone($time, $format, $gmt) {
  $datetime = new DateTime("@$time");
  $timezone = new DateTimeZone('America/New_York');
  $datetime->setTimezone($timezone);
  return $datetime->format($format);
}

Display modified time in hours

Show the time since the post was last modified in hours.

add_filter('get_post_modified_time', 'display_modified_time_in_hours', 10, 3);
function display_modified_time_in_hours($time, $format, $gmt) {
  $time_difference = time() - $time;
  $hours = floor($time_difference / 3600);
  return $hours;
}

Add days to modified time

Add a specific number of days to the post’s modified time.

add_filter('get_post_modified_time', 'add_days_to_modified_time', 10, 3);
function add_days_to_modified_time($time, $format, $gmt) {
  $additional_days = 2;
  $time += $additional_days * 24 * 60 * 60;
  return date($format, $time);
}

Set a fixed modified time

Set a fixed modified time for all posts.

add_filter('get_post_modified_time', 'set_fixed_modified_time', 10, 3);
function set_fixed_modified_time($time, $format, $gmt) {
  $fixed_time = '2023-04-22 12:00:00';
  return date($format, strtotime($fixed_time));
}

Modify modified time only for specific post type

Modify the modified time only for a specific post type, e.g., ‘product’.

add_filter('get_post_modified_time', 'modify_time_for_specific_post_type', 10, 3);
function modify_time_for_specific_post_type($time, $format, $gmt) {
  global $post;
  if ('product' === $post->post_type) {
    // your custom code here
    return $time;
  }
  return $time;
}