Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created August 23, 2022 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/8b1dfae4607b43386c741e083e81d01f to your computer and use it in GitHub Desktop.
Save billerickson/8b1dfae4607b43386c741e083e81d01f to your computer and use it in GitHub Desktop.
<?php
/**
* Calculate the time difference - a replacement for `human_time_diff()` until it is improved.
*
* Based on BuddyPress function `bp_core_time_since()`, which in turn is based on functions created by
* Dunstan Orchard - http://1976design.com
*
* This function will return an text representation of the time elapsed since a
* given date, giving the two largest units e.g.:
*
* - 2 hours and 50 minutes
* - 4 days
* - 4 weeks and 6 days
*
* @since 1.7.0
*
* @param int $older_date Unix timestamp of date you want to calculate the time since for`.
* @param int|bool $newer_date Optional. Unix timestamp of date to compare older date to. Default false (current time).
* @param int $relative_depth Optional, how many units to include in relative date. Default 2.
* @return string The time difference between two dates.
*/
function genesis_human_time_diff( $older_date, $newer_date = false, $relative_depth = 2 ) {
if ( ! is_int( $older_date ) ) {
return '';
}
// If no newer date is given, assume now.
$newer_date = $newer_date ?: time();
// Difference in seconds.
$since = absint( $newer_date - $older_date );
if ( ! $since ) {
return '0 ' . _x( 'seconds', 'time difference', 'genesis' );
}
// Hold units of time in seconds, and their pluralised strings (not translated yet).
$units = [
/* translators: %s: Number of year(s). */
[ 31536000, _nx_noop( '%s year', '%s years', 'time difference', 'genesis' ) ], // 60 * 60 * 24 * 365
/* translators: %s: Number of month(s). */
[ 2592000, _nx_noop( '%s month', '%s months', 'time difference', 'genesis' ) ], // 60 * 60 * 24 * 30
/* translators: %s: Number of week(s). */
[ 604800, _nx_noop( '%s week', '%s weeks', 'time difference', 'genesis' ) ], // 60 * 60 * 24 * 7
/* translators: %s: Number of day(s). */
[ 86400, _nx_noop( '%s day', '%s days', 'time difference', 'genesis' ) ], // 60 * 60 * 24
/* translators: %s: Number of hour(s). */
[ 3600, _nx_noop( '%s hour', '%s hours', 'time difference', 'genesis' ) ], // 60 * 60
/* translators: %s: Number of minute(s). */
[ 60, _nx_noop( '%s minute', '%s minutes', 'time difference', 'genesis' ) ],
/* translators: %s: Number of second(s). */
[ 1, _nx_noop( '%s second', '%s seconds', 'time difference', 'genesis' ) ],
];
// Build output with as many units as specified in $relative_depth.
$relative_depth = (int) $relative_depth ?: 2;
$i = 0;
$counted_seconds = 0;
$date_partials = [];
$amount_date_partials = 0;
$amount_units = count( $units );
while ( $amount_date_partials < $relative_depth && $i < $amount_units ) {
$seconds = $units[ $i ][0];
$count = (int) floor( ( $since - $counted_seconds ) / $seconds );
if ( 0 !== $count ) {
$date_partials[] = sprintf( translate_nooped_plural( $units[ $i ][1], $count, 'genesis' ), $count );
$counted_seconds += $count * $seconds;
$amount_date_partials = count( $date_partials );
}
$i++;
}
if ( empty( $date_partials ) ) {
$output = '';
} elseif ( 1 === count( $date_partials ) ) {
$output = $date_partials[0];
} else {
// Combine all but last partial using commas.
$output = implode( ', ', array_slice( $date_partials, 0, -1 ) );
// Add 'and' separator.
$output .= ' ' . _x( 'and', 'separator in time difference', 'genesis' ) . ' ';
// Add last partial.
$output .= end( $date_partials );
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment