Measure time in ONLY HOUR format

I need to change the timeformat in this column

to be in hours, not measured in seconds and minutes. This meaning that 30 minutes should be measured in 0.5 hours and so on.

I think I need to edit this line of code. Any suggestions on how to change the measurement to ONLY HOURS?

return human_time_diff(strtotime($item['time_login']), strtotime($item['time_last_seen']));

Topic timestamp formatting customization Wordpress

Category Web


The human_time_diff() function is not going to work without modification.

Here's a working solution using simple PHP:

$login = '2020-09-28 10:00:00';
$last_seen = '2020-09-28 09:30:00';

$diff = strtotime($login) - strtotime($last_seen);

$hours = $diff / ( 60 * 60 );

return $hours;

Welcome to WPSE.

You can try to filter it with:

add_filter( 'human_time_diff', 'wpse_hourly_human_time_diff', 10, 4 );

where the filter's callback is e.g.

/**
 * Human time difference in hours only.
 *
 * @param string $since The difference in human readable text.
 * @param int    $diff  The difference in seconds.
 * @param int    $from  Unix timestamp from which the difference begins.
 * @param int    $to    Unix timestamp to end the time difference.
 */
function wpse_hourly_human_time_diff( $since, $diff, $from, $to ){
    return round( $diff / HOUR_IN_SECONDS, 1 );
} 

but you will need to control where you want to apply this filter with:

remove_filter( 'human_time_diff', 'wpse_hourly_human_time_diff', 10 );

and further restrictions.

Hope you can adjust to your needs.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.