bp_core_time_since( int|string $older_date, int|bool $newer_date = false )
Get an English-language representation of the time elapsed since a given date.
Description
Based on function created by Dunstan Orchard – http://1976design.com
This function will return an English representation of the time elapsed since a given date. eg: 2 hours and 50 minutes eg: 4 days eg: 4 weeks and 6 days
Note that fractions of minutes are not represented in the return string. So an interval of 3 minutes will be represented by "3 minutes ago", as will an interval of 3 minutes 59 seconds.
Parameters
- $older_date
-
(Required) The earlier time from which you're calculating the time elapsed. Enter either as an integer Unix timestamp, or as a date string of the format 'Y-m-d h:i:s'.
- $newer_date
-
(Optional) Unix timestamp of date to compare older date to. Default: false (current time).
Default value: false
Return
(string) String representing the time since the older date, eg "2 hours and 50 minutes".
Source
File: bp-core/bp-core-functions.php
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 | function bp_core_time_since( $older_date , $newer_date = false ) { /** * Filters whether or not to bypass BuddyPress' time_since calculations. * * @since BuddyPress 1.7.0 * * @param bool $value Whether or not to bypass. * @param string $older_date Earlier time from which we're calculating time elapsed. * @param string $newer_date Unix timestamp of date to compare older time to. */ $pre_value = apply_filters( 'bp_core_time_since_pre' , false, $older_date , $newer_date ); if ( false !== $pre_value ) { return $pre_value ; } /** * Filters the value to use if the time since is unknown. * * @since BuddyPress 1.5.0 * * @param string $value String representing the time since the older date. */ $unknown_text = apply_filters( 'bp_core_time_since_unknown_text' , __( 'sometime' , 'buddyboss' ) ); /** * Filters the value to use if the time since is right now. * * @since BuddyPress 1.5.0 * * @param string $value String representing the time since the older date. */ $right_now_text = apply_filters( 'bp_core_time_since_right_now_text' , __( 'right now' , 'buddyboss' ) ); /** * Filters the value to use if the time since is some time ago. * * @since BuddyPress 1.5.0 * * @param string $value String representing the time since the older date. */ $ago_text = apply_filters( 'bp_core_time_since_ago_text' , __( '%s ago' , 'buddyboss' ) ); // Array of time period chunks. $chunks = array ( YEAR_IN_SECONDS, 30 * DAY_IN_SECONDS, WEEK_IN_SECONDS, DAY_IN_SECONDS, HOUR_IN_SECONDS, MINUTE_IN_SECONDS, 1 ); 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 ) ? bp_core_current_time( true, 'timestamp' ) : $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 ]; // 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. switch ( $seconds ) { case YEAR_IN_SECONDS : $output = sprintf( _n( '%s year' , '%s years' , $count , 'buddyboss' ), $count ); break ; case 30 * DAY_IN_SECONDS : $output = sprintf( _n( '%s month' , '%s months' , $count , 'buddyboss' ), $count ); break ; case WEEK_IN_SECONDS : $output = sprintf( _n( '%s week' , '%s weeks' , $count , 'buddyboss' ), $count ); break ; case DAY_IN_SECONDS : $output = sprintf( _n( '%s day' , '%s days' , $count , 'buddyboss' ), $count ); break ; case HOUR_IN_SECONDS : $output = sprintf( _n( '%s hour' , '%s hours' , $count , 'buddyboss' ), $count ); break ; case MINUTE_IN_SECONDS : $output = sprintf( _n( '%s minute' , '%s minutes' , $count , 'buddyboss' ), $count ); break ; default : $output = sprintf( _n( '%s second' , '%s seconds' , $count , 'buddyboss' ), $count ); } // Step two: the second chunk // A quirk in the implementation means that this // condition fails in the case of minutes and seconds. // We've left the quirk in place, since fractions of a // minute are not a useful piece of information for our // purposes. if ( $i + 2 < $j ) { $seconds2 = $chunks [ $i + 1]; $count2 = floor ( ( $since - ( $seconds * $count ) ) / $seconds2 ); // Add to output var. if ( 0 != $count2 ) { $output .= _x( ',' , 'Separator in time since' , 'buddyboss' ) . ' ' ; switch ( $seconds2 ) { case 30 * DAY_IN_SECONDS : $output .= sprintf( _n( '%s month' , '%s months' , $count2 , 'buddyboss' ), $count2 ); break ; case WEEK_IN_SECONDS : $output .= sprintf( _n( '%s week' , '%s weeks' , $count2 , 'buddyboss' ), $count2 ); break ; case DAY_IN_SECONDS : $output .= sprintf( _n( '%s day' , '%s days' , $count2 , 'buddyboss' ), $count2 ); break ; case HOUR_IN_SECONDS : $output .= sprintf( _n( '%s hour' , '%s hours' , $count2 , 'buddyboss' ), $count2 ); break ; case MINUTE_IN_SECONDS : $output .= sprintf( _n( '%s minute' , '%s minutes' , $count2 , 'buddyboss' ), $count2 ); break ; default : $output .= sprintf( _n( '%s second' , '%s seconds' , $count2 , 'buddyboss' ), $count2 ); } } } // 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 ); } /** * Filters the English-language representation of the time elapsed since a given date. * * @since BuddyPress 1.7.0 * * @param string $output Final 'time since' string. * @param string $older_date Earlier time from which we're calculating time elapsed. * @param string $newer_date Unix timestamp of date to compare older time to. */ return apply_filters( 'bp_core_time_since' , $output , $older_date , $newer_date ); } |
Changelog
Version | Description |
---|---|
BuddyPress 1.0.0 | 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.