• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Genesis Theme Framework a2z

Genesis Theme Framework a2z

WordPress Dynamic API Reference

  • Home
  • Plugins
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / Parsed Source / Parsed: 50441
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;

}
 

Published: 25th November 2019 | Last updated: 25th November 2019

Primary Sidebar

Information

Source ref: genesis_human_time_diff() – Calculate the time difference – a replacement for `human_time_diff()` until it is improved.
Parse count / Source file date: 1569839049
MD5 hash: 613661af8c7bd7a22627e00e4e31368e

Footer

Genesis a2z
Genesis Theme Framework a2z
WordPress Dynamic API Reference
WordPress 5.9.3
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  genesis.wp-a2z.org
© Copyright Genesis a2z 2014-2022. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites