0

I'm using a plugin called Latest Tweet Widget to display the latest tweet. The tweet date gets displayed by the month and day (Mar 22). However, I want it to be in "Twitter time" meaning words like "3 days ago, 1 hour ago, etc.".

I asked the developer of the plugin at the WordPress forums and he said that I need to "use the latest_tweets_render_date filter to perform my own relative date representation."

The filter in question is here under the "Custom HTML" section (also quoted below): https://wordpress.org/plugins/latest-tweets-widget/other_notes/#Theming

add_filter('latest_tweets_render_date', function( $created_at ){
    $date = DateTime::createFromFormat('D M d H:i:s O Y', $created_at );
    return $date->format('d M h:ia'); }, 10 , 1 );

However, I am new to PHP and unable to understand what exactly needs to be written in order for "Twitter time" to work. Can somebody please show me the way?

1 Answer 1

2

WordPress has a core function human_time_diff that does what you want, using it with the filter you provide you have someting like so:

add_filter('latest_tweets_render_date', function( $created_at ){
    $date = DateTime::createFromFormat('D M d H:i:s O Y', $created_at ); 
    return sprintf( '%s ' . __( 'ago' ), human_time_diff( $date->format('U') ) );
});
3
  • Thanks, G.M. However, I just tried this and latest tweet date is now displaying as "44 years". Any idea what could be setting it back so far?
    – Desi
    Mar 22, 2014 at 20:02
  • I just tried deactivating all of my other plugins and it still shows as "44 years ago" so I'm guessing the problem can be narrowed down to the plugin using a different format than the one documented. Is this something only the plugin author can help me with?
    – Desi
    Mar 22, 2014 at 20:17
  • @Desi just noticed there was an error in my code, sorry. Try now.
    – gmazzap
    Mar 22, 2014 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.