bp_format_time

Format a date based on a UNIX timestamp.

Description

bp_format_time( (string) $time = '', (constant) $exclude_time = false, (bool) $gmt = true ); 

This function can be used to turn a UNIX timestamp into a properly formatted (and possibly localized) string, userful for ouputting the date & time an action took place.

Not to be confused with bp_core_time_since(),, this function is best used for displaying a more exact date and time vs. a human-readable time.

Note: This function may be improved or removed at a later date, as it is hardly used and adds an additional layer of complexity to calculating dates and times together with timezone offsets and i18n.

Parameters (3)

0. $time — Optional. (string) => ''
The UNIX timestamp to be formatted.
1. $exclude_time — Optional. (constant) => false
True to return only the month + day, false to return month, day, and time. Default: false.
2. $gmt — Optional. (bool) => true
True to display in local time, false to leave in GMT. Default: true.

Usage

  1. if ( !function_exists( 'bp_format_time' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-template.php'; 
  3.  
  4. // The UNIX timestamp to be formatted. 
  5. $time = ''; 
  6.  
  7. // Optional. True to return only the month + day, false 
  8. // to return month, day, and time. Default: false. 
  9. $exclude_time = false; 
  10.  
  11. // Optional. True to display in local time, false to 
  12. // leave in GMT. Default: true. 
  13. $gmt = true; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = bp_format_time($time, $exclude_time, $gmt); 
  17.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-template.php  
  1. function bp_format_time( $time = '', $exclude_time = false, $gmt = true ) { 
  2.  
  3. // Bail if time is empty or not numeric 
  4. // @todo We should output something smarter here. 
  5. if ( empty( $time ) || ! is_numeric( $time ) ) { 
  6. return false; 
  7.  
  8. // Get GMT offset from root blog. 
  9. if ( true === $gmt ) { 
  10.  
  11. // Use Timezone string if set. 
  12. $timezone_string = bp_get_option( 'timezone_string' ); 
  13. if ( ! empty( $timezone_string ) ) { 
  14. $timezone_object = timezone_open( $timezone_string ); 
  15. $datetime_object = date_create( "@{$time}" ); 
  16. $timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS; 
  17.  
  18. // Fall back on less reliable gmt_offset. 
  19. } else { 
  20. $timezone_offset = bp_get_option( 'gmt_offset' ); 
  21.  
  22. // Calculate time based on the offset. 
  23. $calculated_time = $time + ( $timezone_offset * HOUR_IN_SECONDS ); 
  24.  
  25. // No localizing, so just use the time that was submitted. 
  26. } else { 
  27. $calculated_time = $time; 
  28.  
  29. // Formatted date: "March 18, 2014". 
  30. $formatted_date = date_i18n( bp_get_option( 'date_format' ), $calculated_time, $gmt ); 
  31.  
  32. // Should we show the time also? 
  33. if ( true !== $exclude_time ) { 
  34.  
  35. // Formatted time: "2:00 pm". 
  36. $formatted_time = date_i18n( bp_get_option( 'time_format' ), $calculated_time, $gmt ); 
  37.  
  38. // Return string formatted with date and time. 
  39. $formatted_date = sprintf( esc_html__( '%1$s at %2$s', buddypress ), $formatted_date, $formatted_time ); 
  40.  
  41. /** 
  42. * Filters the date based on a UNIX timestamp. 
  43. * @since 1.0.0 
  44. * @param string $formatted_date Formatted date from the timestamp. 
  45. */ 
  46. return apply_filters( 'bp_format_time', $formatted_date );