Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Timestamps in members timezone

  • @colabsadmin

    Participant

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

Viewing 14 replies - 1 through 14 (of 14 total)
  • @ubernaut

    Participant

    that’s an interesting idea.

    @colabsadmin

    Participant

    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.

    @colabsadmin

    Participant

    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)

    @shanebp

    Moderator

    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.

    @henrywright-1

    Member

    @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.

    @colabsadmin

    Participant

    @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.

    @shanebp

    Moderator

    @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.

    @colabsadmin

    Participant

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

    @ubernaut

    Participant

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

    🙂

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

    @djpaul

    Keymaster

    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

    @henrywright-1

    Member

    Hey @colabsadmin @shanebp @djpaul

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

    @shanebp

    Moderator

    @colabsadmin @henrywright-1 @djpaul

    Here’s a plugin: https://github.com/shanebp/BP-Timezones

    Currently it only uses this filter hook:
    ‘bp_get_message_thread_last_post_date’

    Hook suggestions ?

    @colabsadmin

    Participant

    @shanebp Sorry for the late reply on this. I didnt have a notification set. Anyhow, thanks for writing this!

    I installed the BP-Timezone plugin and activated it, but the loader is not adding the timezones to the db. The field gets added correctly. I’m running BP 2.0.2. Any ideas?

    As far as hook suggestions, since starting this topic I haven’t come across another place to hook into. Once I get this plugin to install correctly, I’ll hunt for other places to use it.

    Thanks again.

    @colabsadmin

    Participant

    @shanebp Figured out the problem. I have the Buddypress Xprofile Custom Field Type plugin installed. Disabling that allowed the creation of the time zone options.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘[Resolved] Timestamps in members timezone’ is closed to new replies.
Skip to toolbar