Yii 1.1: timeago

updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago") from ISO 8601 formatted dates and times
31 followers

Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago") from ISO 8601 formatted dates and times embedded in your HTML

this is the jquery timeago plugin wrapper class . it is useful for such as comment , may be it should go with commentsModule

Requirements

tested on yii 1.8 ; may works well with previous version .

update history:

  • update to a new version 0.11.3 (mainly enhance the locale message supporting ) and you can download here YiiTimeAgo on github , and any suggestions is welcome :)

Usage

download it , and extract it to protected/extensions dir , then in your view file use it as other standard CWidget class .

this widget just replace the text of specified element (abbr or time tag of html5);

...can use code blocks like the following...

<?php
 
$this->widget('ext.timeago.JTimeAgo', array(
    'selector' => ' .timeago',
 
));
 
?>
 
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
 
<abbr class="timeago" title="<?php echo  date(DATE_ISO8601,time());  ?>">will be replaced</abbr>
 
 
    <?php echo CHtml::openTag('abbr',array('class'=>'timeago',
                     'title'=>date(DATE_ISO8601,time()),
                    ));?>
        <?php // echo date(DATE_ISO8601,time()); ?>
   <?php echo CHtml::closeTag('label'); ?>
 
<time class="timeago" datetime="2008-07-17T09:24:17Z">July 17, 2008</time>
 
<time class="timeago" datetime="2011-07-17">July 17, 2008</time>
    <hr size="2px" />
    <div>
        <p><h3>test settings</h3></p>
 
 
        <?php
        $this->widget('ext.timeago.JTimeAgo', array(
            'selector' => ' .cnTime',
            'settings'=>array(
                'allowFuture'=>true,
                'strings'=>'js:{
  prefixAgo: null,
  prefixFromNow: "从现在开始",
  suffixAgo: "之前",
  suffixFromNow: null,
  seconds: "不到 1 分钟",
  minute: "大约 1 分钟",
  minutes: "%d 分钟",
  hour: "大约 1 小时",
  hours: "大约 %d 小时",
  day: "1 天",
  days: "%d 天",
  month: "大约 1 个月",
  months: "%d 月",
  year: "大约 1 年",
  years: "%d 年",
  numbers: []
}
                ',
            )
        ));
 
        ?>
        <abbr class="cnTime" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
 
 
    </div>

you can specify your language string as my second example showing ( i just use Chinese ) , refer here Locale override and find your own language config there .

Resources

...external resources for this extension...

Total 9 comments

#10445 report it
Cozumel at 2012/10/29 08:28am
afterAjaxUpdate

The example afterAjaxUpdate doesn't seem to work for me, but this does:

<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'afterAjaxUpdate'=>'js:function(id, data) {$("abbr.timeago").timeago();}',
)); ?>

An alternative method I also found is:

<?php 
Yii::app()->clientScript->registerScript('handle_ajax_function', "
function addTime(id,result)
{   
    $('abbr.timeago').timeago(); 
}
");
?>
 
<?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'afterAjaxUpdate'=>'addTime',
)); ?>

Hope this helps someone!

#8291 report it
jiaming at 2012/05/23 07:48am
I read that wiki and...

I read that wiki and I can guarantee I saved UTC time correctly in my Database.

in jquery.timeago.... getTime()should also return the utc time , that means , the

distance between the Jquery's time and my database' time should be correct, like 1 second but not 8 hours!!!

I do have 8 hours time differences between me and UTC. But getTime() in java should return utc seconds but not local time....

I really have no idea about what is going wrong...

#8290 report it
Wiseon3 at 2012/05/23 05:15am
Local time zone
#8288 report it
yiqing95 at 2012/05/23 03:56am
@w00tw00t111 , jzhong5

@Stops Working After Ajax Request :

you have to reinitialize it again in CGridView::afterAjaxUpdate use the original js way :

'afterAjaxUpdate'=>'js: $("abbr.timeago").timeago(); '

@time zone probelm : you see this is just a jquery plugin wrapper ,your problem is php side . for this plugin at most it can according to the current localeto register a different message js file . the rest you should consider use php to settle it . to be frank i had not met any i18n projects . hope this article give you some idea : utc datetime convert

#8286 report it
jiaming at 2012/05/23 02:22am
time zone probelm

i stored utc time in my database..... howeveer, I found that it has problem if I have an international app. It always give me 8 hours ago in china and like 1 hour ago in us

How can I handle that? Since it's international, I can't set a fixed timeline....

Any ideas?

#8204 report it
w00tw00t111 at 2012/05/17 11:51am
Stops Working After Ajax Request

Thank you for this extension - it is great!

Have a question about usage with CJUI widgets. I am using timeago in a CListView widget. After clicking on a separate page (i.e. 'next' or page 2, etc. in the pagination options) time ago no longer works.

Is it possible that the timeago dom script is not reinitialized?

What is the proper usage when using your extension inside an object that will be reloaded via ajax such as in a pagination request?

Thank you!

#7698 report it
Haensel at 2012/04/10 09:28am
Very useful

I love timeago and this extension. Thanks for sharing!

#6298 report it
yiqing95 at 2011/12/27 03:30am
@bonnie you are welcom

happy to help you , as you can see this is just a wrapper class , we all should thank to the original author of this jquery plugin . :)

#6296 report it
bonnie at 2011/12/26 07:31pm
Thanks A lot

This has made my holiday fantastic I have been scratching my head trying to figure out how to do this with ajax. Actually I have a site where people post what they want and I wanted to show how long that post has been there using the create time from the database. This is how I did using your extension and you see how the date is from the database.'F j, Y',$data->create_time

<?php
 
$this->widget('ext.timeago.JTimeAgo', array(
    'selector' => ' .timeago',
 
));
 
?>
<?php echo CHtml::openTag('abbr',array('class'=>'timeago',
                     'title'=>date('F j, Y',$data->create_time),
                    ));?>

Thanks again.

Leave a comment

Please to leave your comment.

Create extension