Yii Framework Forum: Monthly Archives widget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Monthly Archives widget Rate Topic: -----

#1 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 20 June 2009 - 01:07 AM

And I have added the Monthly Archives widget to the blog demo. The code I have added is very concise since I made use of existing code such as Calendar widget. Followings are all the code that is added.

As usual, you can download it from the google code repositry and can try the demo out. Have fun :)

protected/components/MonthlyArchives.php:
<?php
class MonthlyArchives extends Portlet
{
  public $title='Monthly Archives';

  public function findAllPostDate()
  {
    $yearmonth = array();
    $posts = Post::model()->findRecentPosts(PHP_INT_MAX);
    foreach ($posts as $post) {
      $ym = date('M, Y', $post->createTime);
      if (!isset($yearmonth[$ym])) {
        $yearmonth[$ym] = 1;
      } else {
        $yearmonth[$ym]++;
      }
    }
    return $yearmonth;
  }

  protected function renderContent()
  {
    $this->render('monthlyArchives');
  }
}


protected/components/views/monthlyArchives.php:
<ul>
<?php foreach ($this->findAllPostDate() as $month=>$val): ?>
<li>
<?php echo CHtml::link("$month ($val)", CHtml::normalizeUrl(array('post/PostedInMonth',
                                                                  'time'=>strtotime($month),
                                                                  'pnc'=>'c')));  ?>
</li>
<?php endforeach; ?>
</ul>



0

#2 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 20 June 2009 - 11:03 AM

I've found a bug.

$ym = date('M, Y', $post->createTime);

should be

$ym = date('F Y', $post->createTime);

in order for strtotime() function to work correctly. The SVN has already been fixed.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users