Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use human_time_diff filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 21, 2023
5 minutes read

Working with WordPress hooks can be a game-changer, and the human_time_diff filter is no exception. If you ever needed to display a human-readable time difference between two timestamps, this filter is your best friend. Let’s dive into how you can use this hook effectively and see some live examples to get you started.

Example 1: Modify Time Difference Format

Sometimes, the default format isn’t quite what you need. Here’s how you can modify the output format of the time difference using the human_time_diff filter.

    function weplugins_modify_human_time_diff_format($since, $diff, $from, $to) {
        // Custom logic to change the $since format
        return strtoupper($since);
    }
    add_filter("human_time_diff", "weplugins_modify_human_time_diff_format", 10, 4);
    

Example 2: Conditional Time Display

Want to display different messages based on the time difference? You can achieve this with a little conditional logic.

    function weplugins_conditional_human_time_diff($since, $diff, $from, $to) {
        if ($diff < 3600) { // Less than an hour
            return "Less than an hour ago";
        }
        return $since;
    }
    add_filter("human_time_diff", "weplugins_conditional_human_time_diff", 10, 4);
    

Example 3: Removing the Filter

There might be instances where you need to remove a previously registered filter to restore default behavior. Here’s how you can do it:

    remove_filter("human_time_diff", "weplugins_modify_human_time_diff_format", 10, 4);
    

Make sure you provide the same callback function name, priority, and number of arguments while removing the hook callback.

Below is the basic usage of the hook:

    apply_filters('human_time_diff', string $since, int $diff, int $from, int $to);
    

Access Premium WordPress Plugins

Contact Us

Need any customization or facing issues using this hook? Feel free to contact us. Our team at WePlugins is always ready to assist you!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.