Skip to:
Content
Pages
Categories
Search
Top
Bottom

Some quick code help using the activity ‘since’ parameter


  • Andrew
    Participant

    @snd26

    I just found out there was a parameter called ‘since’ to use with the activity loop to get activities since a certain date and time.

    ‘since’ => // Y-m-d H:i:s

    Just looking for some quick help with this time function I created. I’m not great with code and don’t understand the workings of how WordPress/Buddypress works with the date and time, but I wanted to create a function that converts ‘hours ago’ into the Y-m-d H:i:s format so it can be used with the ‘since’ parameter.

    function get_time_since( $hours_ago='' ) { 
    
    	// Local date and time right now
    	$current_date = date('Y-m-d H:i:s');
    	
    	// convert 'hours ago' to 'Y-m-d H:i:s'
    	$date = date_create($current_date);
    	date_sub($date, date_interval_create_from_date_string($hours_ago));
    	
    	return date_format($date, 'Y-m-d H:i:s');
    }
    // example use
    // display posts in the last 6 hours
    	function my_filter_activity( $loop ) { 
    
    		if ( is_page( 'custom-page' )) {
    			$loop['since'] = get_time_since( '6 hours' );
    		}
    
    		return $loop;
    	}
    	add_filter( 'bp_after_has_activity_parse_args', 'my_filter_activity' );

    This works correctly where I am. The only thing I’m confused about is I don’t know if I have to detect a user’s time zone in the function for it to work correctly?

  • You must be logged in to reply to this topic.
Skip to toolbar