Change friendly time format

How would I change the "posted 1234 Days ago", to "posted on Dec 20, 2011" on Elgg 1.8? Thanks

  • I did that and now it spitting out the raw code instead of the date, I don't know how programmers have the patients lol

    Here is my start.php


    elgg_register_event_handler('init', 'system', 'friendlytime_init');

    function friendlytime_init() {


    Here is my friendlytime.php

    $friendly_time = date("M j, Y", ($vars['time']);
    $timestamp = htmlspecialchars(date(elgg_echo('friendlytime:date_format'), $vars['time']));
    echo "<acronym title=\"$timestamp\">$friendly_time";

  • 2 errors that I see:

    You don't have a closing paranthesis on date(), 

    $friendly_time = date("M j, Y", $vars['time']);

    You are not closing acronym tag:

    echo "<acronym title=\"$timestamp\">$friendly_time</acronym>";


    Yes, patience is a virtue.

  • But, imagine how proud you are going to be when you actually achieve the desired result. You will then want to prove @Trajan wrong, and post your plugin in the community for everyone to use ;)

  • I agree very much so! I added the fixes to the code, am I to add "<?php" to the top of friendlytime.php? I tried it and got a white page, with it the raw code is still shown on the page.

    My friendlytime.php

    $friendly_time = date()("M j, Y", ($vars['time']);

    $timestamp = htmlspecialchars(date(elgg_echo('friendlytime:date_format'), $vars['time']));


    echo "$friendly_time";

  • The closing acronym tag is at the en but this site cuts it out

  • @Chris,

    The above code is php, and it therefore should be wrapped in <?php ?>. In case of Elgg, you don't need the closing tag ?>  unless you are parsing html, which is not the case.


    Your friendly time should look like:


    $friendly_time = date("M j, Y", $vars['time']); // Please note the brackets

    echo $friendly_time;


  • Awesome, works great! I'll package it up on the weekend with credit to you. Is there a way to add back in the "just now", yesturday, then the date if after yesturday? Also, the activity on the main page stil shows the days.only when you click on the wire does it show dates. And lastly, is there a way to add an "at" to the date? So it shows as posted on Dec 21, 2011 at 2:26pm? A appreciate all the help. :)

  • Congrats on your first Elgg success Chris.

    You can have the "just now" etc. for recent posts with a bit of logic

    $friendly_time = date(("M j, Y", ($vars['time']));

    if($vars['time'] > (time() - 60*60*48)){

    $friendly_time = htmlspecialchars(date(elgg_echo('friendlytime:date_format'), $vars['time']));


    echo "$friendly_time";


    If you want the "at" in there, the easy way is to change your date format - see

    date("M j, Y \a\t g:ia", $vars['time']);


    Of course that doesn't make it translateable.  To make it translateable you'll need to register a language file and pass the time in parts.

  • Good job!

    Here you have two tasks ahead of you:

    1. Create a proper language key => value pair

    2. Create a plugin hook to overwrite the default representation of friendly time



    Create a folder called 'languages' and add 'en.php' file (see any other plugin for an example)

    Add a language key => value pair

    'friendly_time:date_format:at' => 'at %s'

    %s in the value represents a replacable value, it can be parsed using elgg_echo() function

    $friendly_time = elgg_echo('friendly_time:date_format:at', array($friendly_time));

    You can add as many '%s' to your language strings, they are replaced consequently by array() values in your elgg_echo() function



    The reference you are making to other friendly times. This means they were generated by a function, not by the view. 

    If you look in engine/lib/output.php you will find that function. It's called elgg_get_friendly_time(). When you study the function, you will see that it's return can be overridden by a plugin_hook:

    $result = elgg_trigger_plugin_hook('format', 'friendly:time', $params, NULL);

    So, what we want to do is overwrite the output of that function.

    In our start.php, in the init function we register a plugin hook. A plugin hook will be triggered by the above and return the value of a custom function we are about to write:


    function myplugin_init() {

    elgg_register_plugin_hook_handler('format', 'friendly:time', 'my_function_to_overwrite_default_time');



    Now we need to define my_function_to_overwrite_default_time; this needs to be done outside of your myplugin_init().


    function my_function_to_overwrite_default_time($hook, $type, $return, $params) {

    // plugin hooks automatically get 4 variables

    // $hook is the name of the plugin hook (in this case 'format')

    // $type is the type of the plugin hook (in this case 'friendly:time')

    // $return is the last value received from other plugin hooks that were implemented before yours (in this case NULL)

    // $params is an array of params passed to the plugin hook (in this case $params = array('time' => $time))


    $old_time = $params['time'];

    $new_time = date("M j, Y", $old_time);

    $new_time_readable = elgg_echo('friendly_time', array($new_time));

    return $new_time_readable;



Feedback and Planning

Feedback and Planning

Discussions about the past, present, and future of Elgg and this community site.