How to automatically delete item after date expires?

4 posts by 3 authors in: Forums > CMS Builder
Last Post: February 22, 2012   (RSS)

Re: [eriqcook] How to automatically delete item after date expires?

By Dave - February 15, 2012

Hi Eriq,

Would it work to just hide the content after several days? If so you could use a special 'removeDate' field.

See: http://www.interactivetools.com/docs/cmsbuilder/special_fieldnames.html

It will be visible and editable in the Admin but not on the website.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [eriqcook] How to automatically delete item after date expires?

By gkornbluth - February 16, 2012

Hi eric,

If you have an event (over) date in your record you could also compare that date with the current date and decide to not show that record if the over date is more than three days more than the current date.

Here's a portion of a recipe from a recipe in my CMSB Cookbook. http://www.thecmsbcookbook.com that compares dates.


COMPARING DATES


COMPARING DATES
When combined with the “if” statement, this extremely useful concept can be used to show or hide fields based on a particular date, automatically archive records after a specified time period and perform many other functions.

Here’s the basic idea of how to compare a date field to today’s date on a list page. This example uses a date field called “opening_reception” in a multi record editor called “exhibitions”. The goal was to automatically hide the opening reception date after the date had passed.

First you’ll need a “foreach” statement to display each record.

<?php foreach ($exhibitionsRecords as $record): ?>

Since dates are normally expressed as months, days and years are hard to compare mathematically, the dates are converted to the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

Note that dates are referenced to local time as set in the CMSB “General Settings” information and not server time.

Dave suggested to first define a few variables and to use simple, descriptive names so that the process is easier to follow. Then set some rules for the comparisons. You can use mathematical operators like <, >, <= or >= between values to compare them in different ways.

<?php
$eventUnixTime = strtotime( $record['reception_date'] ); // seconds since 1970
$eventDateString = date("l, F jS", $eventUnixTime); // example format: Monday, June 1st
$currentUnixTime = time();
$currentDateString = date("l, F jS", $currentUnixTime);


$isEventToday = ($eventDateString == $currentDateString); // first comparison
$isEventOver = !$isEventToday && ($eventUnixTime < $currentUnixTime); // second comparison
$isFutureEvent = !$isEventOver && !$isEventToday; // third comparison
?>


Then it becomes a simple matter of comparing the variables
<?php if ($isFutureEvent): ?>
Opening Reception: <?php echo $eventDateString ?>

<?php elseif ($isEventToday): ?>
The Opening reception is today. Don't miss it!!

<?php else: ?>
Sorry, you missed the Opening Reception.

<?php endif; ?>

and don’t forget the endforeach statement to close your loop.

<?php endforeach ?>

Here are some ways to add or subtract days or months to a time
$add 3 days = strtotime(date('Y-m-d', strtotime("+3 day")));
$add_a_week = strtotime(date('Y-m-d', strtotime("+1 week")));
$subtract_2_months = strtotime(date('Y-m-d', strtotime("-2 month")));


there’s a lot more information about date functions at:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

It's a bit confusing, but I hope that gets you started.
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] How to automatically delete item after date expires?

By eriqcook - February 22, 2012

Thank you very much everyone. I will give this a try!