Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('get_the_time', 'my_get_the_time', 100, 3 );
- function my_get_the_time($the_time, $d, $post){
- return findTimeAgo( get_post_time('U', true, $post) );
- }
- function findTimeAgo( $past ) {
- // creates the "time ago" string. This always starts with an "about..."
- $timeAgo = "";
- // finds the time difference
- $timeDifference = time() - $past;
- // less than 29secs
- if($timeDifference <= 29) {
- $timeAgo = "less than a minute";
- }
- // more than 29secs and less than 1min29secss
- else if($timeDifference > 29 && $timeDifference <= 89) {
- $timeAgo = "1 minute";
- }
- // between 1min30secs and 44mins29secs
- else if($timeDifference > 89 &&
- $timeDifference <= ((MINUTE_IN_SECONDS * 44) + 29)
- ) {
- $minutes = floor($timeDifference / MINUTE_IN_SECONDS);
- $timeAgo = $minutes." minutes";
- }
- // between 44mins30secs and 1hour29mins29secs
- else if(
- $timeDifference > ((MINUTE_IN_SECONDS * 44) + 29)
- &&
- $timeDifference < ((MINUTE_IN_SECONDS * 89) + 29)
- ) {
- $timeAgo = "about 1 hour";
- }
- // between 1hour29mins30secs and 23hours59mins29secs
- else if(
- $timeDifference > (
- (MINUTE_IN_SECONDS * 89) +
- 29
- )
- &&
- $timeDifference <= (
- (HOUR_IN_SECONDS * 23) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- ) {
- $hours = floor($timeDifference / HOUR_IN_SECONDS);
- $timeAgo = $hours." hours";
- }
- // between 23hours59mins30secs and 47hours59mins29secs
- else if(
- $timeDifference > (
- (HOUR_IN_SECONDS * 23) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- &&
- $timeDifference <= (
- (HOUR_IN_SECONDS * 47) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- ) {
- $timeAgo = "1 day";
- }
- // between 47hours59mins30secs and 29days23hours59mins29secs
- else if(
- $timeDifference > (
- (HOUR_IN_SECONDS * 47) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- &&
- $timeDifference <= (
- (DAY_IN_SECONDS * 29) +
- (HOUR_IN_SECONDS * 23) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- ) {
- $days = floor($timeDifference / DAY_IN_SECONDS);
- $timeAgo = $days." days";
- }
- // between 29days23hours59mins30secs and 59days23hours59mins29secs
- else if(
- $timeDifference > (
- (DAY_IN_SECONDS * 29) +
- (HOUR_IN_SECONDS * 23) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- &&
- $timeDifference <= (
- (DAY_IN_SECONDS * 59) +
- (HOUR_IN_SECONDS * 23) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- ) {
- $timeAgo = "about 1 month";
- }
- // between 59days23hours59mins30secs and 1year (minus 1sec)
- else if(
- $timeDifference > (
- (DAY_IN_SECONDS * 59) +
- (HOUR_IN_SECONDS * 23) +
- (MINUTE_IN_SECONDS * 59) +
- 29
- )
- &&
- $timeDifference < YEAR_IN_SECONDS
- ) {
- $months = round($timeDifference / MONTH_IN_SECONDS);
- // if months is 1, then set it to 2, because we are "past" 1 month
- if($months == 1) {
- $months = 2;
- }
- $timeAgo = $months." months";
- }
- // between 1year and 2years (minus 1sec)
- else if(
- $timeDifference >= YEAR_IN_SECONDS
- &&
- $timeDifference < (YEAR_IN_SECONDS * 2)
- ) {
- $timeAgo = "about 1 year";
- }
- // 2years or more
- else {
- $years = floor($timeDifference / YEAR_IN_SECONDS);
- $timeAgo = "over ".$years." years";
- }
- return $timeAgo." ago";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement