Comment Date, When Posted ?

Hi everybody,

I have a new request for you,

How could I have this kind of date format (time elapsed format) :

Posted 1 week ago

Posted 1 hour ago

Posted now

Something existing ?

It should be on the afterFind() in the AR

My Model is :

Comment

id

text

userId

createTime

EDIT

I found this code, but it showing everytime 44 year




function time_elapsed_string() {

    $etime = time() - $this->createdTime;

    

    if ($etime < 1) {

        return '0 seconds';

    }

    

    $a = array( 12 * 30 * 24 * 60 * 60  =>  'year',

                30 * 24 * 60 * 60       =>  'month',

                24 * 60 * 60            =>  'day',

                60 * 60                 =>  'hour',

                60                      =>  'minute',

                1                       =>  'second'

                );

    

    foreach ($a as $secs => $str) {

        $d = $etime / $secs;

        if ($d >= 1) {

            $r = round($d);

            return $r . ' ' . $str . ($r > 1 ? 's' : '');

        }

    }

}



EDIT RESOLVED :

It Works I just deleted my afterFind() method :)

Well for that. in a Model class you can also define your own method and can use that in your view file or in Grids like




In a view file :

$model->formattedTime();


And define method in model class like


public function formattedTime(){

   

// logic for showing timestamp in formatted output

  

  return $formattedDate;

}



i hope it would help you. And for logic i think you can Refer this link

Just prepare on function and pass created date to that. i.e,




function getCreated($created)

{

	// Here you can use core php function to check interval from today date and created date.

	// And using conditional code you can return the value/data/string as you want.

	return $createdValue;

}

Ok thanks guys

I found the solution ;)

Yeah that’s sounds great ;) So, how did you do this task. Could you please share your code.if its something better then i will also opt it. :)