Automatic news archive

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 19, 2014   (RSS)

By mizrahi - May 19, 2014

I would like to have the news area on a site automatically archive. I want everything within the past 12 months to display on a "Recent News" page, and everything older than 12 months to display on a "News Archive" page. Can someone assist me with the database calls for this? Here is the basic query...

// news items
  list($news_itemsRecords, $news_itemsMetaData) = getRecords(array(
    'tableName'   => 'news_items',
    'loadUploads' => true,
    'allowSearch' => false,
    'perPage'     => '20',
    'where'    => "(removeDate >= NOW() OR removeDate='0000-00-00 00:00:00')",
    'ignoreRemoveDate' =>  true,
  )); 

By gregThomas - May 19, 2014

Hi mizrahi,

The following getRecords function should retrieve records that were created after the current date last year:

  // news items
  list($news_itemsRecords, $news_itemsMetaData) = getRecords(array(
    'tableName'   => 'news_items',
    'loadUploads' => true,
    'allowSearch' => false,
    'perPage'     => '20',
    'where'    =>   "(removeDate >= NOW() OR removeDate='0000-00-00 00:00:00') AND (`createdDate` >= '".date('Y-m-d H:i:s', strtotime('-1 year'))."')"
    'ignoreRemoveDate' =>  true,
  )); 

Then this function would retrieve records that are older than a year:

  // news items
  list($news_itemsRecords, $news_itemsMetaData) = getRecords(array(
    'tableName'   => 'news_items',
    'loadUploads' => true,
    'allowSearch' => false,
    'perPage'     => '20',
    'where'    =>   "`createdDate` < '".date('Y-m-d H:i:s', strtotime('-1 year'))."')"
    'ignoreRemoveDate' =>  true,
  )); 

So the strtotime function can take a string that tell it to carry out some basic date math (for example '2013-03-10 - 1 year', '+1 week ', etc).

I've used this function to get the date from last year formatted for MySQL. You might need to change the date column that this statement searches. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com