Skip to:
Content
Pages
Categories
Search
Top
Bottom

Timestamps in members timezone

  • Avatar of colabsadmin
    colabsadmin
    Participant

    @colabsadmin

    Anyway to display timestamps so that they are relative to the members timezone and the not the servers timezone?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Avatar of @ubernaut
    @ubernaut
    Participant

    @ubernaut

    that’s an interesting idea.

    Avatar of colabsadmin
    colabsadmin
    Participant

    @colabsadmin

    I came up with something that’s working. I’ll post it once I fully test it, but basically I’ve added a profile field for the member to choose their timezone. Then in my child theme, I call my own function that uses DateTime and setTimeZone to convert the message date to the users timezone. It defaults to the severs timezone if they haven’t supplied their timezone. My users seem pleased with it.

    Avatar of colabsadmin
    colabsadmin
    Participant

    @colabsadmin

    Here’s how I did this.

    First, I created the “Time Zone” select box for xProfile by putting the code in the following link into my functions.php file.

    http://docs.taptappress.com/buddypress-add-time-zone-list-profile-field/

    Once functions.php is run once, it will add the Time Zone field to the user profile. Once its been added, you can delete everything except the $zones array (you’ll need it later).

    I then created a new function to look up the users time zone and display the last thread date based on that time zone. Put this in your functions.php file.

    function mycustom_get_message_thread_last_post_date() {
    	global $messages_template;
    	$timezone = mycustom_get_member_timezone();
    	if ($timezone != '' ) {	
    		$timestamp = '@' . strtotime($messages_template->thread->last_message_date); 
    		$dt = new DateTime( $timestamp );
    		$dt->setTimeZone(new DateTimeZone($timezone));
    		return $dt->format('M j, Y, g:i a'); /* format the displayed timestamp however you want */
    	} else {
    		return bp_format_time( strtotime( $messages_template->thread->last_message_date ) );
    	}
    }
    
    function mycustom_get_member_timezone() {
    /** put the $zones array from the first step here.  I've left it out to save space in this post **/
       return array_search(bp_get_member_profile_data('field=Time Zone'), $zones);	
    }

    In a child theme, add/edit /members/single/messages/message-loop.php to use your new custom function. There’s two places you need to do this (inbox and sent).

    Replace

    <span class="activity"><?php bp_message_thread_last_post_date(); ?></span>

    With

    <span class="activity"><?php echo mycustom_get_message_thread_last_post_date(); ?></span>

    That’s it.

    Note: Technically you can do without the array if you want, but I wanted to display the GMT offset to the user instead of ‘US/Alaska’ (for example)

    Avatar of shanebpdev
    shanebp
    Participant

    @shanebp

    Nice. It would also be interesting to provide this on all timestamps.

    One issue ‘tho – in this:

    http://docs.taptappress.com/buddypress-add-time-zone-list-profile-field/

    I think you’re inserting / updating all those xprofile fields on every page load.
    Why not just do it once?

    You could write a plugin that uses ‘register_activation_hook’ to run bp_add_custom_timezone_list() once.

    And then use the filter hook in:
    bp_get_message_thread_last_post_date()
    to adjust the timestamp without requiring a template change.

    And you could use other hooks to adjust other timestamp displays.

    That would be a popular and very handy plugin.

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    @colabsadmin I really like the idea of this. It definitely beats having relative dates everywhere, which is what I did. So many BP sites out there these days with members in different time zones crying out for a plugin like this.

    Avatar of colabsadmin
    colabsadmin
    Participant

    @colabsadmin

    @shanebp. I only have it there until the field is added, then I remove it

    “Once functions.php is run once, it will add the Time Zone field to the user profile. Once its been added, you can delete everything except the $zones array (you’ll need it later).”

    I’m new to wordpress/buddypress so writing a plugin isn’t going to happen anytime soon, but would love to see it done.

    Avatar of shanebpdev
    shanebp
    Participant

    @shanebp

    @colabsadmin

    Ah, didn’t notice that instruction, just looked at the code.
    ( It’s surprising how many plugins leave in that kind of code. )

    >I’m new to wordpress/buddypress so writing a plugin isn’t going to happen anytime soon

    You’ve already done a good chunk of the work.
    If you and/or @henrywright-1 want to draft a plugin, I’d be willing to get involved.

    Avatar of colabsadmin
    colabsadmin
    Participant

    @colabsadmin

    Cool. It would be a pretty good way to learn how to write a plugin :)

    Avatar of @ubernaut
    @ubernaut
    Participant

    @ubernaut

    it’s not difficult even i have one and i don’t know lick of php.
    :)

    http://wordpress.org/plugins/color-help/

    I like this idea. It would make a fab plugin if someone can package it up well.

    I’m not sure BP core needs a timezone field by default, but if we can do anything to make filtering/re-calculating the dates easier (e.g. add a filter, or so), don’t hesitate to ask! Create an enhancement request on https://buddypress.trac.wordpress.org and I’ll get it into BP 2.0

    Avatar of Henry
    Henry
    Participant

    @henrywright-1

    Hey @colabsadmin @shanebp @djpaul

    I’m happy to help out writing the plugin. Anyone feel like taking the lead?

Viewing 11 replies - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.