bp_format_time( int|string $time = '', bool $exclude_time = false, bool $gmt = true )

Format a date based on a UNIX timestamp.

Description

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

$time

(int|string) (Optional) The UNIX timestamp to be formatted.

Default value: ''

$exclude_time

(bool) (Optional) True to return only the month + day, false to return month, day, and time. Default: false.

Default value: false

$gmt

(bool) (Optional) True to display in local time, false to leave in GMT. Default: true.

Default value: true

Return

(mixed) A string representation of $time, in the format "March 18, 2014 at 2:00 pm" (or whatever your 'date_format' and 'time_format' settings are on your root blog). False on failure.

Source

File: bp-core/bp-core-template.php

function bp_format_time( $time = '', $exclude_time = false, $gmt = true ) {

	// Bail if time is empty or not numeric
	// @todo We should output something smarter here.
	if ( empty( $time ) || ! is_numeric( $time ) ) {
		return false;
	}

	// Get GMT offset from root blog.
	if ( true === $gmt ) {

		// Use Timezone string if set.
		$timezone_string = bp_get_option( 'timezone_string' );
		if ( ! empty( $timezone_string ) ) {
			$timezone_object = timezone_open( $timezone_string );
			$datetime_object = date_create( "@{$time}" );
			$timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;

		// Fall back on less reliable gmt_offset.
		} else {
			$timezone_offset = bp_get_option( 'gmt_offset' );
		}

		// Calculate time based on the offset.
		$calculated_time = $time + ( $timezone_offset * HOUR_IN_SECONDS );

	// No localizing, so just use the time that was submitted.
	} else {
		$calculated_time = $time;
	}

	// Formatted date: "March 18, 2014".
	$formatted_date = date_i18n( bp_get_option( 'date_format' ), $calculated_time, $gmt );

	// Should we show the time also?
	if ( true !== $exclude_time ) {

		// Formatted time: "2:00 pm".
		$formatted_time = date_i18n( bp_get_option( 'time_format' ), $calculated_time, $gmt );

		// Return string formatted with date and time.
		$formatted_date = sprintf( esc_html__( '%1$s at %2$s', 'buddyboss' ), $formatted_date, $formatted_time );
	}

	/**
	 * Filters the date based on a UNIX timestamp.
	 *
	 * @since BuddyPress 1.0.0
	 *
	 * @param string $formatted_date Formatted date from the timestamp.
	 */
	return apply_filters( 'bp_format_time', $formatted_date );
}

Changelog

Changelog
Version Description
BuddyPress 1.1.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.