Last active
June 7, 2020 15:38
-
-
Save ImtiazEpu/0f5bd8615284d43d829c686eb6115e17 to your computer and use it in GitHub Desktop.
human readable time fuction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Time to human readable time | |
* | |
* @param $ts | |
* @param string $fallback_format | |
* | |
* @return false|string | |
*/ | |
public function time2str( $ts, $fallback_format = 'M j, Y H:i' ) { | |
if ( ! ctype_digit( $ts ) ) { | |
$ts = strtotime( $ts ); | |
} | |
$diff = time() - $ts; | |
if ( $diff == 0 ) { | |
return esc_html__( 'now', 'cbxchangelog' ); | |
} elseif ( $diff > 0 ) { | |
$day_diff = floor( $diff / 86400 ); | |
if ( $day_diff == 0 ) { | |
if ( $diff < 60 ) { | |
return esc_html__( 'just now', 'cbxchangelog' ); | |
} | |
if ( $diff < 120 ) { | |
return esc_html__( '1 minute ago', 'cbxchangelog' ); | |
} | |
if ( $diff < 3600 ) { | |
return sprintf( esc_html__( '%s minutes ago', 'cbxchangelog' ), floor( $diff / 60 ) ); | |
} | |
if ( $diff < 7200 ) { | |
return esc_html__( '1 hour ago', 'cbxchangelog' ); | |
} | |
if ( $diff < 86400 ) { | |
return floor( $diff / 3600 ) . ' hours ago'; | |
} | |
} | |
if ( $day_diff == 1 ) { | |
return esc_html__( 'Yesterday', 'cbxchangelog' ); | |
} | |
if ( $day_diff < 7 ) { | |
return sprintf( esc_html__( '%s days ago', 'cbxchangelog' ), $day_diff ); | |
} | |
if ( $day_diff < 31 ) { | |
return sprintf( esc_html__( '%s weeks ago', 'cbxchangelog' ), ceil( $day_diff / 7 ) ); | |
} | |
if ( $day_diff < 60 ) { | |
return esc_html__( 'last month', 'cbxchangelog' ); | |
} | |
return date( $fallback_format, $ts ); | |
} else { | |
$diff = abs( $diff ); | |
$day_diff = floor( $diff / 86400 ); | |
if ( $day_diff == 0 ) { | |
if ( $diff < 120 ) { | |
return esc_html__( 'in a minute', 'cbxchangelog' ); | |
} | |
if ( $diff < 3600 ) { | |
return sprintf( esc_html__( 'in %s minutes', 'cbxchangelog' ), floor( $diff / 60 ) ); | |
} | |
if ( $diff < 7200 ) { | |
return esc_html__( 'in an hour', 'cbxchangelog' ); | |
} | |
if ( $diff < 86400 ) { | |
return sprintf( esc_html__( 'in %s hours', 'cbxchangelog' ), floor( $diff / 3600 ) ); | |
} | |
} | |
if ( $day_diff == 1 ) { | |
return esc_html__( 'Tomorrow', 'cbxchangelog' ); | |
} | |
if ( $day_diff < 4 ) { | |
return date( 'l', $ts ); | |
} | |
if ( $day_diff < 7 + ( 7 - date( 'w' ) ) ) { | |
return esc_html__( 'next week', 'cbxchangelog' ); | |
} | |
if ( ceil( $day_diff / 7 ) < 4 ) { | |
return sprintf( esc_html__( 'in %s weeks', 'cbxchangelog' ), ceil( $day_diff / 7 ) ); | |
} | |
if ( date( 'n', $ts ) == date( 'n' ) + 1 ) { | |
return esc_html__( 'next month', 'cbxchangelog' ); | |
} | |
return date( $fallback_format, $ts ); | |
} | |
}//end time2str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment