Change friendly time format

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

  • In order to understand a little more about time, here are some suggestions:

    1. date and time are stored as timestamps. Current time stamp is something like 1324500573. This equals to the number of seconds that passed since January 1, 1970.

    2. The date() function helps you convert a timestamp to a human readable format. Since we are used to reading dates in various formats, the date function accepts a number of formatting options. Try to decode "M j, Y" using the chart here: http://de3.php.net/manual/en/function.date.php

    3. To calculate the difference in timestamps you can use 24 * 60 * 60 (one day in timestamp = 24 hours * 60 minutes * 60 seconds). For example, you can figure out if the event happened more than 5 minutes ago by:

    $current_time = time(); // php function to get current time

    if ($current_time - $event_time > 5 * 60) {

    echo 'It has been more than 5 minutes';

    } else if ($current_time - $event_time == 5 *60) {

    echo 'It has been exactly 5 minutes';

    }

  • Thanks so much, I will take a look tomorrow and get back to you on Friday, life calls now lol

  • There's a plugins available that will display friendly time:

    http://community.elgg.org/pg/plugins/project/497405/developer/demola/improved-friendly-time

    It has been written for Elgg 1.7 but I think it might still work in Elgg 1.8 with some minor adjustments.

  • @Ismayil - I didn't know about the friendly:time plugin hook, thanks

    @Chris - the plugin hook method that Ismayil just explained, while seemingly more complicated at first, is indeed the better way to handle the conversion.

  • @Chris: well done on making your first plugin for elgg. :)

    @Ismayil: not sure why I need to be proven wrong.

    @Core Devs: not sure why my post was removed for advertising. No where did I say I would do it for a fee. I said that if the issue was urgent someone could help him out, and if so there was a specific place that he should go to talk about that if he wanted to. That's guidance...not advertising.

  • Weird, I would not have considered that advertising at all...  I think someone's got an over-active advertising perception filter.

  • I have no idea who did that or what the original post was.

  • Ah well, nevermind...these things happen sometimes, and it's possible that the comment could have been construed as advertising when taken out of context. I'll make sure next time I'm extremely clear to avoid that.

  • Ok, I added e language file and the other code to te output.php, no prob, now here is the code I came up with for start.php, and I get a shite page.

    <?php

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

    function friendlytime_init() {

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

    elgg_register_plugin_hook_handler('format', 'friendly:time', NULL, 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;

    }

  • event_handler for 'init', 'system' should be outside of the friendlytime_init function. If you look closer, you will realize that the event_handler is calling the function called friendlytime_init

    You don't need to trigger a plugin_hook. On the contrary, you need to register a plugin hook handler to perform certain activities on a plugin_hook triggered in the Elgg core.

    Aslo make sure you don't have friendlytime_init twice in your start.php

    <?php

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

    function friendlytime_init() {

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

    }

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

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