<p>You could write a fairly simple plugin to automatically set the expiration based off of another field value or calculation.</p>

<p>In your plugin's main file:</p>

<pre><code>public function init() {

    parent::init();

    craft()-&gt;on('entries.saveEntry', function(Event $event) {

        $entry = $event-&gt;params['entry'];

        if ($entry-&gt;section == 'Events') {

            if (empty($entry-&gt;expiryDate)) {
                $date = new DateTime($entry-&gt;dateTime);
                $modifiedDate = date_modify($date, 'tomorrow 5am');
                $entry-&gt;expiryDate = $modifiedDate;
                craft()-&gt;entries-&gt;saveEntry($entry);
            }

        }

    });

}
</code></pre>

<p><strong>Updated Code</strong> fixed syntax errors and added a check to see if expiration date is already set, otherwise created an endless loop (and may overwrite a manual setting).</p>

CC BY-SA 3.0
2015-04-26T00:07:34.890Z
You're on fire today, Douglas... I can't keep up.
Brad Bell
CC BY-SA 3.0
2015-04-25T23:04:31.370Z
2015-04-30T03:52:59.483Z
2015-04-30T03:52:59.483Z
Douglas McDonald
<p>Can I somehow set the expiration date of an entry based on a custom-field. I ask this because I don't want the site-owner to have to manually set the expiration date (for multiple reasons)but they are setting a 'start' date in a custom field. I am currently outputting entries and limiting them by doing this:</p>

<pre><code>{% for event in entries %}
                    {% set count = 0 %}
                    {% if count &lt; 5 and event.dateTime|date_modify('tomorrow 5am')|date('U') &gt; now|date('U') %}
</code></pre>

<p>It would be really fantastic though if i was able to use that same date_modify to expire the entry and remove the entries from the loop all-together due to being expired.</p>

<datetime><expiration>
Dates Expiring By Custom Field date_modify
# Dates Expiring By Custom Field date_modify

(Asked by: username_0 on 2015-04-25)

Can I somehow set the expiration date of an entry based on a custom-field. I ask this because I don't want the site-owner to have to manually set the expiration date (for multiple reasons)but they are setting a 'start' date in a custom field. I am currently outputting entries and limiting them by doing this:
{% for event in entries %}
                    {% set count = 0 %}
                    {% if count < 5 and event.dateTime|date_modify('tomorrow 5am')|date('U') > now|date('U') %}

It would be really fantastic though if i was able to use that same date_modify to expire the entry and remove the entries from the loop all-together due to being expired.

**Tags:** datetime, expiration


---

## 1 answers

### Answer by username_1 on 2015-04-25. Score: 3 | Accepted answer

You could write a fairly simple plugin to automatically set the expiration based off of another field value or calculation.
In your plugin's main file:
public function init() {

    parent::init();

    craft()->on('entries.saveEntry', function(Event $event) {

        $entry = $event->params['entry'];

        if ($entry->section == 'Events') {

            if (empty($entry->expiryDate)) {
                $date = new DateTime($entry->dateTime);
                $modifiedDate = date_modify($date, 'tomorrow 5am');
                $entry->expiryDate = $modifiedDate;
                craft()->entries->saveEntry($entry);
            }

        }

    });

}

Updated Code fixed syntax errors and added a check to see if expiration date is already set, otherwise created an endless loop (and may overwrite a manual setting).

> **Comments:**
> **username_2**: You're on fire today, Douglas... I can't keep up.  

---

