Changeset 12931 for trunk/src/bp-core/bp-core-functions.php
- Timestamp:
- 04/29/2021 10:20:39 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/bp-core-functions.php
r12929 r12931 1178 1178 1179 1179 /** 1180 * Get an English-language representation of the time elapsed since a given date.1180 * Calculate the human time difference between two dates. 1181 1181 * 1182 1182 * Based on function created by Dunstan Orchard - http://1976design.com 1183 1183 * 1184 * This function will return an English representation of the time elapsed 1185 * since a given date. 1186 * eg: 2 hours and 50 minutes 1187 * eg: 4 days 1188 * eg: 4 weeks and 6 days 1189 * 1190 * Note that fractions of minutes are not represented in the return string. So 1191 * an interval of 3 minutes will be represented by "3 minutes ago", as will an 1192 * interval of 3 minutes 59 seconds. 1193 * 1194 * @since 1.0.0 1195 * 1196 * @param int|string $older_date The earlier time from which you're calculating 1197 * the time elapsed. Enter either as an integer Unix timestamp, 1198 * or as a date string of the format 'Y-m-d h:i:s'. 1199 * @param int|bool $newer_date Optional. Unix timestamp of date to compare older 1200 * date to. Default: false (current time). 1201 * @return string String representing the time since the older date, eg 1202 * "2 hours and 50 minutes". 1203 */ 1204 function bp_core_time_since( $older_date, $newer_date = false ) { 1205 1206 /** 1207 * Filters whether or not to bypass BuddyPress' time_since calculations. 1208 * 1209 * @since 1.7.0 1210 * 1211 * @param bool $value Whether or not to bypass. 1212 * @param string $older_date Earlier time from which we're calculating time elapsed. 1213 * @param string $newer_date Unix timestamp of date to compare older time to. 1214 */ 1215 $pre_value = apply_filters( 'bp_core_time_since_pre', false, $older_date, $newer_date ); 1216 if ( false !== $pre_value ) { 1217 return $pre_value; 1218 } 1219 1220 /** 1221 * Filters the value to use if the time since is unknown. 1222 * 1223 * @since 1.5.0 1224 * 1225 * @param string $value String representing the time since the older date. 1226 */ 1227 $unknown_text = apply_filters( 'bp_core_time_since_unknown_text', __( 'sometime', 'buddypress' ) ); 1228 1229 /** 1230 * Filters the value to use if the time since is right now. 1231 * 1232 * @since 1.5.0 1233 * 1234 * @param string $value String representing the time since the older date. 1235 */ 1236 $right_now_text = apply_filters( 'bp_core_time_since_right_now_text', __( 'right now', 'buddypress' ) ); 1237 1238 /** 1239 * Filters the value to use if the time since is some time ago. 1240 * 1241 * @since 1.5.0 1242 * 1243 * @param string $value String representing the time since the older date. 1244 */ 1245 $ago_text = apply_filters( 1246 'bp_core_time_since_ago_text', 1247 /* translators: %s: the human time diff. */ 1248 __( '%s ago', 'buddypress' ) 1184 * @since 8.0.0 1185 * 1186 * @param array $args { 1187 * An array of arguments. All arguments are technically optional. 1188 * 1189 * @type int|string $older_date An integer Unix timestamp or a date string of the format 'Y-m-d h:i:s'. 1190 * @type int|string $newer_date An integer Unix timestamp or a date string of the format 'Y-m-d h:i:s'. 1191 * @type int $time_chunks The number of time chunks to get (1 or 2). 1192 * } 1193 * @return null|array|false Null if there's no time diff. An array containing 1 or 2 chunks 1194 * of human time. False if travelling into the future. 1195 */ 1196 function bp_core_time_diff( $args = array() ) { 1197 $retval = null; 1198 $r = wp_parse_args( 1199 $args, 1200 array( 1201 'older_date' => 0, 1202 'newer_date' => bp_core_current_time( true, 'timestamp' ), 1203 'time_chunks' => 2, 1204 ) 1249 1205 ); 1250 1206 … … 1260 1216 ); 1261 1217 1262 if ( !empty( $older_date ) && !is_numeric( $older_date ) ) { 1263 $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) ); 1264 $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) ); 1265 $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] ); 1266 } 1267 1268 /** 1269 * $newer_date will equal false if we want to know the time elapsed between 1270 * a date and the current time. $newer_date will have a value if we want to 1271 * work out time elapsed between two known dates. 1272 */ 1273 $newer_date = ( !$newer_date ) ? bp_core_current_time( true, 'timestamp' ) : $newer_date; 1218 foreach ( array( 'older_date', 'newer_date' ) as $date ) { 1219 if ( ! $r[ $date ] ) { 1220 continue; 1221 } 1222 1223 if ( ! is_numeric( $r[ $date ] ) ) { 1224 $time_chunks = explode( ':', str_replace( ' ', ':', $r[ $date ] ) ); 1225 $date_chunks = explode( '-', str_replace( ' ', '-', $r[ $date ] ) ); 1226 $r[ $date ] = gmmktime( 1227 (int) $time_chunks[1], 1228 (int) $time_chunks[2], 1229 (int) $time_chunks[3], 1230 (int) $date_chunks[1], 1231 (int) $date_chunks[2], 1232 (int) $date_chunks[0] 1233 ); 1234 } 1235 } 1274 1236 1275 1237 // Difference in seconds. 1276 $since = $newer_date - $older_date; 1277 1278 // Something went wrong with date calculation and we ended up with a negative date. 1279 if ( 0 > $since ) { 1280 $output = $unknown_text; 1281 1282 /** 1283 * We only want to output two chunks of time here, eg: 1284 * x years, xx months 1285 * x days, xx hours 1286 * so there's only two bits of calculation below: 1287 */ 1288 } else { 1289 1238 $diff = $r['newer_date'] - $r['older_date']; 1239 1240 /** 1241 * We only want to return one or two chunks of time here, eg: 1242 * - `array( 'x years', 'xx months' )`, 1243 * - `array( 'x days', 'xx hours' )`. 1244 * So there's only two bits of calculation below. 1245 */ 1246 if ( 0 <= $diff && (int) $r['time_chunks'] ) { 1290 1247 // Step one: the first chunk. 1291 1248 for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) { … … 1293 1250 1294 1251 // Finding the biggest chunk (if the chunk fits, break). 1295 $count = floor( $ since/ $seconds );1252 $count = floor( $diff / $seconds ); 1296 1253 if ( 0 != $count ) { 1297 1254 break; … … 1299 1256 } 1300 1257 1301 // If $i iterates all the way to $j, then the event happened 0 seconds ago. 1302 if ( !isset( $chunks[$i] ) ) { 1303 $output = $right_now_text; 1304 1305 } else { 1306 1307 // Set output var. 1258 // Add the first chunk of time diff. 1259 if ( isset( $chunks[ $i ] ) ) { 1260 $retval = array(); 1261 1308 1262 switch ( $seconds ) { 1309 1263 case YEAR_IN_SECONDS : 1310 1264 /* translators: %s: the number of years. */ 1311 $ output = sprintf( _n( '%s year', '%s years',$count, 'buddypress' ), $count );1265 $retval[] = sprintf( _n( '%s year', '%s years', $count, 'buddypress' ), $count ); 1312 1266 break; 1313 1267 case 30 * DAY_IN_SECONDS : 1314 1268 /* translators: %s: the number of months. */ 1315 $ output = sprintf( _n( '%s month', '%s months',$count, 'buddypress' ), $count );1269 $retval[] = sprintf( _n( '%s month', '%s months', $count, 'buddypress' ), $count ); 1316 1270 break; 1317 1271 case WEEK_IN_SECONDS : 1318 1272 /* translators: %s: the number of weeks. */ 1319 $ output = sprintf( _n( '%s week', '%s weeks',$count, 'buddypress' ), $count );1273 $retval[]= sprintf( _n( '%s week', '%s weeks', $count, 'buddypress' ), $count ); 1320 1274 break; 1321 1275 case DAY_IN_SECONDS : 1322 1276 /* translators: %s: the number of days. */ 1323 $ output = sprintf( _n( '%s day', '%s days',$count, 'buddypress' ), $count );1277 $retval[] = sprintf( _n( '%s day', '%s days', $count, 'buddypress' ), $count ); 1324 1278 break; 1325 1279 case HOUR_IN_SECONDS : 1326 1280 /* translators: %s: the number of hours. */ 1327 $ output = sprintf( _n( '%s hour', '%s hours',$count, 'buddypress' ), $count );1281 $retval[] = sprintf( _n( '%s hour', '%s hours', $count, 'buddypress' ), $count ); 1328 1282 break; 1329 1283 case MINUTE_IN_SECONDS : 1330 1284 /* translators: %s: the number of minutes. */ 1331 $ output= sprintf( _n( '%s minute', '%s minutes', $count, 'buddypress' ), $count );1285 $retval[] = sprintf( _n( '%s minute', '%s minutes', $count, 'buddypress' ), $count ); 1332 1286 break; 1333 1287 default: 1334 1288 /* translators: %s: the number of seconds. */ 1335 $ output= sprintf( _n( '%s second', '%s seconds', $count, 'buddypress' ), $count );1289 $retval[] = sprintf( _n( '%s second', '%s seconds', $count, 'buddypress' ), $count ); 1336 1290 } 1337 1291 1338 // Step two: the second chunk 1339 // A quirk in the implementation means that this 1340 // condition fails in the case of minutes and seconds. 1341 // We've left the quirk in place, since fractions of a 1342 // minute are not a useful piece of information for our 1343 // purposes. 1344 if ( $i + 2 < $j ) { 1292 /** 1293 * Step two: the second chunk. 1294 * 1295 * A quirk in the implementation means that this condition fails in the case of minutes and seconds. 1296 * We've left the quirk in place, since fractions of a minute are not a useful piece of information 1297 * for our purposes. 1298 */ 1299 if ( 2 === (int) $r['time_chunks'] && $i + 2 < $j ) { 1345 1300 $seconds2 = $chunks[$i + 1]; 1346 $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); 1347 1348 // Add to output var. 1349 if ( 0 != $count2 ) { 1350 $output .= _x( ',', 'Separator in time since', 'buddypress' ) . ' '; 1301 $count2 = floor( ( $diff - ( $seconds * $count ) ) / $seconds2 ); 1302 1303 // Add the second chunk of time diff. 1304 if ( 0 !== (int) $count2 ) { 1351 1305 1352 1306 switch ( $seconds2 ) { 1353 1307 case 30 * DAY_IN_SECONDS : 1354 1308 /* translators: %s: the number of months. */ 1355 $ output .= sprintf( _n( '%s month', '%s months',$count2, 'buddypress' ), $count2 );1309 $retval[] = sprintf( _n( '%s month', '%s months', $count2, 'buddypress' ), $count2 ); 1356 1310 break; 1357 1311 case WEEK_IN_SECONDS : 1358 1312 /* translators: %s: the number of weeks. */ 1359 $ output .= sprintf( _n( '%s week', '%s weeks',$count2, 'buddypress' ), $count2 );1313 $retval[] = sprintf( _n( '%s week', '%s weeks', $count2, 'buddypress' ), $count2 ); 1360 1314 break; 1361 1315 case DAY_IN_SECONDS : 1362 1316 /* translators: %s: the number of days. */ 1363 $ output .= sprintf( _n( '%s day', '%s days',$count2, 'buddypress' ), $count2 );1317 $retval[] = sprintf( _n( '%s day', '%s days', $count2, 'buddypress' ), $count2 ); 1364 1318 break; 1365 1319 case HOUR_IN_SECONDS : 1366 1320 /* translators: %s: the number of hours. */ 1367 $ output .= sprintf( _n( '%s hour', '%s hours',$count2, 'buddypress' ), $count2 );1321 $retval[] = sprintf( _n( '%s hour', '%s hours', $count2, 'buddypress' ), $count2 ); 1368 1322 break; 1369 1323 case MINUTE_IN_SECONDS : 1370 1324 /* translators: %s: the number of minutes. */ 1371 $ output .= sprintf( _n( '%s minute', '%s minutes', $count2, 'buddypress' ), $count2 );1325 $retval[] = sprintf( _n( '%s minute', '%s minutes', $count2, 'buddypress' ), $count2 ); 1372 1326 break; 1373 1327 default: 1374 1328 /* translators: %s: the number of seconds. */ 1375 $ output .= sprintf( _n( '%s second', '%s seconds', $count2, 'buddypress' ), $count2 );1329 $retval[] = sprintf( _n( '%s second', '%s seconds', $count2, 'buddypress' ), $count2 ); 1376 1330 } 1377 1331 } 1378 1332 } 1379 1380 // No output, so happened right now.1381 if ( ! (int) trim( $output ) ) {1382 $output = $right_now_text;1383 }1384 1333 } 1385 } 1386 1387 // Append 'ago' to the end of time-since if not 'right now'. 1388 if ( $output != $right_now_text ) { 1389 $output = sprintf( $ago_text, $output ); 1334 } else { 1335 // Something went wrong with date calculation and we ended up with a negative date. 1336 $retval = false; 1337 } 1338 1339 return $retval; 1340 } 1341 1342 /** 1343 * Get an English-language representation of the time elapsed since a given date. 1344 * 1345 * This function will return an English representation of the time elapsed 1346 * since a given date. 1347 * eg: 2 hours, 50 minutes 1348 * eg: 4 days 1349 * eg: 4 weeks, 6 days 1350 * 1351 * Note that fractions of minutes are not represented in the return string. So 1352 * an interval of 3 minutes will be represented by "3 minutes ago", as will an 1353 * interval of 3 minutes 59 seconds. 1354 * 1355 * @since 1.0.0 1356 * @since 8.0.0 Move the time difference calculation into `bp_core_time_diff()`. 1357 * 1358 * @param int|string $older_date The earlier time from which you're calculating 1359 * the time elapsed. Enter either as an integer Unix timestamp, 1360 * or as a date string of the format 'Y-m-d h:i:s'. 1361 * @param int|bool $newer_date Optional. Unix timestamp of date to compare older 1362 * date to. Default: false (current time). 1363 * @return string String representing the time since the older date, eg 1364 * "2 hours, 50 minutes". 1365 */ 1366 function bp_core_time_since( $older_date, $newer_date = false ) { 1367 1368 /** 1369 * Filters whether or not to bypass BuddyPress' time_since calculations. 1370 * 1371 * @since 1.7.0 1372 * 1373 * @param bool $value Whether or not to bypass. 1374 * @param string $older_date Earlier time from which we're calculating time elapsed. 1375 * @param string $newer_date Unix timestamp of date to compare older time to. 1376 */ 1377 $pre_value = apply_filters( 'bp_core_time_since_pre', false, $older_date, $newer_date ); 1378 if ( false !== $pre_value ) { 1379 return $pre_value; 1380 } 1381 1382 $newer_date = (int) $newer_date; 1383 $args = array( 1384 'older_date' => $older_date, 1385 ); 1386 1387 if ( $newer_date) { 1388 $args['newer_date'] = $newer_date; 1389 } 1390 1391 // Calculate the time difference. 1392 $time_diff = bp_core_time_diff( $args ); 1393 1394 /** 1395 * Filters the value to use if the time since is some time ago. 1396 * 1397 * @since 1.5.0 1398 * 1399 * @param string $value String representing the time since the older date. 1400 */ 1401 $ago_text = apply_filters( 1402 'bp_core_time_since_ago_text', 1403 /* translators: %s: the human time diff. */ 1404 __( '%s ago', 'buddypress' ) 1405 ); 1406 1407 /** 1408 * Filters the value to use if the time since is right now. 1409 * 1410 * @since 1.5.0 1411 * 1412 * @param string $value String representing the time since the older date. 1413 */ 1414 $output = apply_filters( 'bp_core_time_since_right_now_text', __( 'right now', 'buddypress' ) ); 1415 1416 if ( is_array( $time_diff ) ) { 1417 $separator = _x( ',', 'Separator in time since', 'buddypress' ) . ' '; 1418 $diff_text = implode( $separator, $time_diff ); 1419 $output = sprintf( $ago_text, $diff_text ); 1420 } elseif ( false === $time_diff ) { 1421 /** 1422 * Filters the value to use if the time since is unknown. 1423 * 1424 * @since 1.5.0 1425 * 1426 * @param string $value String representing the time since the older date. 1427 */ 1428 $unknown_text = apply_filters( 'bp_core_time_since_unknown_text', __( 'sometime', 'buddypress' ) ); 1429 $output = sprintf( $ago_text, $unknown_text ); 1390 1430 } 1391 1431 … … 1400 1440 */ 1401 1441 return apply_filters( 'bp_core_time_since', $output, $older_date, $newer_date ); 1442 } 1443 1444 /** 1445 * Get an age to display according to the birth date. 1446 * 1447 * @since 8.0.0 1448 * 1449 * @param int|string $birth_date A timestamp or a MySQL formatted date. 1450 * @return string The age to display. 1451 */ 1452 function bp_core_time_old( $birth_date ) { 1453 $time_diff = bp_core_time_diff( array( 'older_date' => $birth_date, 'time_chunks' => 1 ) ); 1454 $retval = '—'; 1455 1456 if ( $time_diff ) { 1457 $age = reset( $time_diff ); 1458 1459 /** 1460 * Filters the value to use to display the age. 1461 * 1462 * @since 8.0.0 1463 * 1464 * @param string $value String representing the time since the older date. 1465 * @param int $age The age. 1466 */ 1467 $age_text = apply_filters( 1468 'bp_core_time_old_text', 1469 /* translators: %d: the age . */ 1470 __( '%s old', 'buddypress' ), 1471 $age 1472 ); 1473 1474 $retval = sprintf( $age_text, $age ); 1475 } 1476 1477 return $retval; 1402 1478 } 1403 1479
Note: See TracChangeset
for help on using the changeset viewer.