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 think this is right? it's not because I STILL get a white page, ugh, how fustrating!

    <?php

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

    function friendlytime_init() {

    elgg_register_plugin_hook_handler('format', 'friendly:time', 'NULL', params = array('time' => $time));

    }

    function friendlytime_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;

    }

  • elgg_register_plugin_hook_handler('format', 'friendly:time', 'NULL', params = array('time' => $time));

     

    Is the problem

    Should be:

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

    also, you have $time thown in there a bunch but I don't see it defined.  You probably want time() instead.

  • Here is my new code for start.php, still getting a white page

     

    <?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('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;

    }

     

    here is my output.php

    <?php

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

     

    any ideas? thanks for the help amd merry christmas.

     

  • If you're getting a completely white page with no output, there's most likely a php error in there.  Easiest way to determine that is to check the server logs.

    If you're using the plugin hook method you shouldn't need to overwrite output.php.  You should delete output.php from your plugin and run upgrade.php

    Also I just noticed that your handler is hard coding it's variables, should be

    friendlytime_hook_handler($hook, $type, $returnvalue, $params){

    as those values are passed by the trigger function.  That shouldn't account for the white screen though.

    Check the server log and see what it says.

  • ok, STILL a white page

    my 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;

    }

    --------------------------------------

    my friendlytime.php located in views/default/output

    <?php

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

    -------------------------------------

    my en.php located in languages

    <?php

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

    --------------------------------------

    the php error log reads

    [24-Dec-2011 16:28:09] PHP Parse error: syntax error, unexpected T_DOUBLE_ARROW in /XXX/XXXX/public_html/mod/friendlytime/languages/en.php on line 5

  • ok, so the problem lies with your en.php.  Have a look at another language file and you'll see that it starts with the declaration of an array, and ends with a function registering the translation.  You need those parts as well.

    I did mention before, but with the plugin hook method you do not need /views/default/output/friendlytime.php anymore.

     

    So...

    1. Delete friendlytime.php

     

    2. Make your plugin hook handler return the value you want:

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

    $old_time = $params['time'];

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

    $hm = date("g:ia", $old_time);

    $new_time_readable = elgg_echo('friendly_time', array($mdy, $hm));

    return $new_time_readable;

    }

     

    3. Make your en.php work properly

    $english = array(

    'friendly_time' => '%s at %s',

    );

    add_translation("en",$english);

     

    Merry Christmas!

  • Worked great! Thanks so much, you and Ismayil are geniuses. Would it be hard to now add the "just now", "yesturday" etc?

  • No that's not hard to do, all you have to do is run a check in your hook to determine at what point we replace the output

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

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

        // this time is within the last 48 hours, so return the default value

        return $return;

      }

    // everything below this happened longer than 2 days ago, so return a date & time

    $old_time = $params['time'];

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

    $hm = date("g:ia", $old_time);

    $new_time_readable = elgg_echo('friendly_time', array($mdy, $hm));

    return $new_time_readable;

    }

     

  • Hey there, this doesn't need an "if" or "else" statement like in the default output.php file? I've been looking at it but am not sure how to implement it, I woul also need to add mor language arrays no? Like for "today at %s" and so on?

  • No, it should work as-is.

    There is an "if" statement at the beginning, and if true it does: return $return

    That means it will do the default - which is the "yesterday" or "3 hours ago" etc.

    The language strings for that are already present in the core.

Feedback and Planning

Feedback and Planning

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