Automatic news archive

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

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

By mizrahi - May 19, 2014

Thanks.

FYI, the first getRecords worked fine, though it was missing a comma at the end of the where

The second getRecords didn't work, until I added back the "removeDate" fields, and the missing comma.