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...
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.
@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 . :)
Very useful
I love timeago and this extension. Thanks for sharing!
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!
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?
@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
Local time zone
@jzhong5 Have a look at http://www.yiiframework.com/wiki/197/local-time-zones-and-locales/
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...
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!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.