最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Measure time in ONLY HOUR format

programmeradmin6浏览0评论

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']));

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']));
Share Improve this question edited Sep 28, 2020 at 10:58 birgire 68k7 gold badges120 silver badges252 bronze badges asked Sep 28, 2020 at 9:26 Sindre FjellestadSindre Fjellestad 1
Add a comment  | 

2 Answers 2

Reset to default 0

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.

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;
发布评论

评论列表(0)

  1. 暂无评论