bbp_get_time_since( string $older_date, string $newer_date = false, int $gmt = false )
Return formatted time to display human readable time difference.
Description
Parameters
- $older_date
-
(Required) Unix timestamp from which the difference begins.
- $newer_date
-
(Optional) Unix timestamp from which the difference ends. False for current time.
Default value: false
- $gmt
-
(Optional) Whether to use GMT timezone. Default is false.
Default value: false
Return
(string) Formatted time
Source
File: bp-forums/common/functions.php
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | function bbp_get_time_since( $older_date , $newer_date = false, $gmt = false ) { // Setup the strings $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text' , __( 'sometime' , 'buddyboss' ) ); $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text' , __( 'right now' , 'buddyboss' ) ); $ago_text = apply_filters( 'bbp_core_time_since_ago_text' , __( '%s ago' , 'buddyboss' ) ); // array of time period chunks $chunks = array ( array ( 60 * 60 * 24 * 365 , __( 'year' , 'buddyboss' ), __( 'years' , 'buddyboss' ) ), array ( 60 * 60 * 24 * 30 , __( 'month' , 'buddyboss' ), __( 'months' , 'buddyboss' ) ), array ( 60 * 60 * 24 * 7, __( 'week' , 'buddyboss' ), __( 'weeks' , 'buddyboss' ) ), array ( 60 * 60 * 24 , __( 'day' , 'buddyboss' ), __( 'days' , 'buddyboss' ) ), array ( 60 * 60 , __( 'hour' , 'buddyboss' ), __( 'hours' , 'buddyboss' ) ), array ( 60 , __( 'minute' , 'buddyboss' ), __( 'minutes' , 'buddyboss' ) ), array ( 1, __( 'second' , 'buddyboss' ), __( 'seconds' , 'buddyboss' ) ) ); if ( ! empty ( $older_date ) && ! is_numeric ( $older_date ) ) { $time_chunks = explode ( ':' , str_replace ( ' ' , ':' , $older_date ) ); $date_chunks = explode ( '-' , str_replace ( ' ' , '-' , $older_date ) ); $older_date = gmmktime ( (int) $time_chunks [1], (int) $time_chunks [2], (int) $time_chunks [3], (int) $date_chunks [1], (int) $date_chunks [2], (int) $date_chunks [0] ); } // $newer_date will equal false if we want to know the time elapsed // between a date and the current time. $newer_date will have a value if // we want to work out time elapsed between two known dates. $newer_date = ( ! $newer_date ) ? strtotime ( current_time( 'mysql' , $gmt ) ) : $newer_date ; // Difference in seconds $since = $newer_date - $older_date ; // Something went wrong with date calculation and we ended up with a negative date. if ( 0 > $since ) { $output = $unknown_text ; // We only want to output two chunks of time here, eg: // x years, xx months // x days, xx hours // so there's only two bits of calculation below: } else { // Step one: the first chunk for ( $i = 0, $j = count ( $chunks ); $i < $j ; ++ $i ) { $seconds = $chunks [ $i ][0]; // Finding the biggest chunk (if the chunk fits, break) $count = floor ( $since / $seconds ); if ( 0 != $count ) { break ; } } // If $i iterates all the way to $j, then the event happened 0 seconds ago if ( !isset( $chunks [ $i ] ) ) { $output = $right_now_text ; } else { // Set output var $output = ( 1 == $count ) ? '1 ' . $chunks [ $i ][1] : $count . ' ' . $chunks [ $i ][2]; // Step two: the second chunk if ( $i + 2 < $j ) { $seconds2 = $chunks [ $i + 1][0]; $name2 = $chunks [ $i + 1][1]; $count2 = floor ( ( $since - ( $seconds * $count ) ) / $seconds2 ); // Add to output var if ( 0 != $count2 ) { $output .= ( 1 == $count2 ) ? _x( ',' , 'Separator in time since' , 'buddyboss' ) . ' 1 ' . $name2 : _x( ',' , 'Separator in time since' , 'buddyboss' ) . ' ' . $count2 . ' ' . $chunks [ $i + 1][2]; } } // No output, so happened right now if ( ! (int) trim( $output ) ) { $output = $right_now_text ; } } } // Append 'ago' to the end of time-since if not 'right now' if ( $output != $right_now_text ) { $output = sprintf( $ago_text , $output ); } return apply_filters( 'bbp_get_time_since' , $output , $older_date , $newer_date ); } |
Changelog
Version | Description |
---|---|
bbPress (r2544) | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.