wordpress 显示文章已发布多久,显示 min hour day week nonth year
,这个更人性化~
使用方法为 bushwick_time_diff( get_the_time( 'U' ) )
即可返回时间..
<code>
if ( ! function_exists( 'bushwick_time_diff' ) ) :
/**
* Determines the difference between two timestamps.
*
* The difference is returned in a human readable format such as "1 hour",
* "5 mins", "2 days".
*
* @param int $from Unix timestamp from which the difference begins.
* @param int $to Optional. Unix timestamp to end the time difference. Default becomes current time if not set.
* @return string Human readable time difference.
*/
function bushwick_time_diff( $from, $to = '' ) {
$month_in_seconds = 30 * DAY_IN_SECONDS;
if ( empty( $to ) )
$to = current_time( 'timestamp' );
$diff = (int) abs( $to - $from );
if ( $diff <= HOUR_IN_SECONDS ) {
$mins = round( $diff / MINUTE_IN_SECONDS );
if ( $mins <= 1 ) {
$mins = 1;
}
/* translators: min=minute */
$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
} elseif ( ( $diff <= DAY_IN_SECONDS ) && ( $diff > HOUR_IN_SECONDS ) ) {
$hours = round( $diff / HOUR_IN_SECONDS );
if ( $hours <= 1 ) {
$hours = 1;
}
$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
} elseif ( ( $diff <= WEEK_IN_SECONDS ) && ( $diff > DAY_IN_SECONDS ) ) {
$days = round( $diff / DAY_IN_SECONDS );
if ( $days <= 1 ) {
$days = 1;
}
$since = sprintf( _n( '%s day', '%s days', $days ), $days );
} elseif ( ( $diff <= $month_in_seconds ) && ( $diff > WEEK_IN_SECONDS ) ) {
$weeks = round( $diff / WEEK_IN_SECONDS );
if ( $weeks <= 1 ) {
$weeks = 1;
}
$since = sprintf( _n( '%s week', '%s weeks', $weeks, 'bushwick' ), $weeks );
} elseif ( ( $diff <= YEAR_IN_SECONDS ) && ( $diff > $month_in_seconds ) ) {
$months = round( $diff / $month_in_seconds );
if ( $months <= 1 ) {
$months = 1;
}
$since = sprintf( _n( '%s month', '%s months', $months, 'bushwick' ), $months );
} elseif ( $diff >= YEAR_IN_SECONDS ) {
$years = round( $diff / YEAR_IN_SECONDS );
if ( $years <= 1 ) {
$years = 1;
}
$since = sprintf( _n( '%s year', '%s years', $years, 'bushwick' ), $years );
}
return $since;
}
endif;
</code>
if ( ! function_exists( 'bushwick_time_diff' ) ) :
/**
* Determines the difference between two timestamps.
*
* The difference is returned in a human readable format such as "1 hour",
* "5 mins", "2 days".
*
* @param int $from Unix timestamp from which the difference begins.
* @param int $to Optional. Unix timestamp to end the time difference. Default becomes current time if not set.
* @return string Human readable time difference.
*/
function bushwick_time_diff( $from, $to = '' ) {
$month_in_seconds = 30 * DAY_IN_SECONDS;
if ( empty( $to ) )
$to = current_time( 'timestamp' );
$diff = (int) abs( $to - $from );
if ( $diff <= HOUR_IN_SECONDS ) {
$mins = round( $diff / MINUTE_IN_SECONDS );
if ( $mins <= 1 ) {
$mins = 1;
}
/* translators: min=minute */
$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
} elseif ( ( $diff <= DAY_IN_SECONDS ) && ( $diff > HOUR_IN_SECONDS ) ) {
$hours = round( $diff / HOUR_IN_SECONDS );
if ( $hours <= 1 ) {
$hours = 1;
}
$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
} elseif ( ( $diff <= WEEK_IN_SECONDS ) && ( $diff > DAY_IN_SECONDS ) ) {
$days = round( $diff / DAY_IN_SECONDS );
if ( $days <= 1 ) {
$days = 1;
}
$since = sprintf( _n( '%s day', '%s days', $days ), $days );
} elseif ( ( $diff <= $month_in_seconds ) && ( $diff > WEEK_IN_SECONDS ) ) {
$weeks = round( $diff / WEEK_IN_SECONDS );
if ( $weeks <= 1 ) {
$weeks = 1;
}
$since = sprintf( _n( '%s week', '%s weeks', $weeks, 'bushwick' ), $weeks );
} elseif ( ( $diff <= YEAR_IN_SECONDS ) && ( $diff > $month_in_seconds ) ) {
$months = round( $diff / $month_in_seconds );
if ( $months <= 1 ) {
$months = 1;
}
$since = sprintf( _n( '%s month', '%s months', $months, 'bushwick' ), $months );
} elseif ( $diff >= YEAR_IN_SECONDS ) {
$years = round( $diff / YEAR_IN_SECONDS );
if ( $years <= 1 ) {
$years = 1;
}
$since = sprintf( _n( '%s year', '%s years', $years, 'bushwick' ), $years );
}
return $since;
}
endif;
</code>