BuddyDev

Search

How to add a filter to bp_core_current_time() function and set gmt to OFF

  • Participant
    Level: Initiated
    Posts: 6
    Kevin on #53349

    I am trying to modify the time core function to set $gmt to off and let buddypress store the local time and not the GMT or UTC.

    
    function bp_core_current_time( $gmt = true, $type = 'mysql' ) {
    
    	/**
    	 * Filters the current GMT time to save into the DB.
    	 *
    	 * @since 1.2.6
    	 *
    	 * @param string $value Current GMT time.
    	 */
    	return apply_filters( 'bp_core_current_time', current_time( $type, $gmt ) );
    

    I tried several filters but none of them works. What am I doing wrong?

    
    add_filter('bp_core_current_time','set_gmt');
    function set_gmt ($type,$gmt) {
    	return current_time($type='mysql',$gmt=true);
    }
    
  • Participant
    Level: Initiated
    Posts: 6
    Kevin on #53350

    When I hardcode it like this (just change gmt to false it works) so hopefully you are able to correct my filter:

    
    function bp_core_current_time( $gmt = false, $type = 'mysql' ) {
    
    	/**
    	 * Filters the current GMT time to save into the DB.
    	 *
    	 * @since 1.2.6
    	 *
    	 * @param string $value Current GMT time.
    	 */
    	return apply_filters( 'bp_core_current_time', current_time( $type, $gmt ) );
    }
    

    Might this be a bp bug because my wp timezone was set to my local timezone – same for UTC plus offset – and I found other non resolved ancient posts on this?
    https://buddypress.org/support/topic/activity-wall-time-setting/
    https://buddypress.org/support/topic/possible-solution-found-every-single-time-stamp-on-my-site-is-off-by-7-hours/page/3/

  • Keymaster
    (BuddyDev Team)
    Posts: 25203
    Brajesh Singh on #53372

    Hi Kevin,
    Welcome to BuddyDev.

    You need to update your code for filtering.
    There is a problem with this.

    
    add_filter('bp_core_current_time','set_gmt');
    function set_gmt ($type,$gmt) {
    	return current_time($type='mysql',$gmt=true);
    }
    

    It should be

    
    add_filter('bp_core_current_time','set_gmt');
    function set_gmt ($time) {
    	return current_time('mysql', false );
    }
    

    and even better(prefixing function to avoid conflict)

    
    add_filter('bp_core_current_time','kevin_set_current_time_local_tz');
    function kevin_set_current_time_local_tz ($time) {
    	return current_time('mysql',false);
    }
    
    

    Please give it a try.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 6
    Kevin on #53428

    I changed the code but get an php error when calling the page afterwards:

    Fatal error: Uncaught TypeError: date(): Argument #2 ($timestamp) must be of type ?int, string given in \wp-content\plugins\buddypress\bp-core\bp-core-functions.php:1775 Stack trace: #0 \wp-content\plugins\buddypress\bp-core\bp-core-functions.php(1775): date(‘Y-m-d H:i:s’, ‘2024-10-29 19:1…’) #1 \wp-includes\class-wp-hook.php(324):

  • Keymaster
    (BuddyDev Team)
    Posts: 25203
    Brajesh Singh on #53434

    Hi Kevin,
    Thank you for the reply.

    I am sorry, I see the issue now.

    I am sorry but there is no solution currently. BuddyPress does not pass the $type to the filter, so you would not know if the current time should return a timestamp or date time string.

    Please disable the above code I have provided. It will be problematic as It can not provide timestamp.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved