<p>The problem you have here is you are calling saveElement from EVENT_AFTER_SAVE_ELEMENT which is causing an infinite loop.</p>

<p>You need to set:</p>

<pre><code>$entry-&gt;propagating = true;
</code></pre>

<p>Before calling the saveElement method.</p>

<p>Dates should be a PHP DateTime class, e.g:</p>

<pre><code>$entry-&gt;rangeStart = new \DateTime('2019-04-10 19:00:00');
</code></pre>

CC BY-SA 4.0
2019-04-11T10:41:02.690Z
Ahh I was so set on date that I didn't see the loop, however setting $entry->propagating = true; didn't help. I've switched to before save event instead and now it works.
Lukigi
CC BY-SA 4.0
2019-04-10T18:01:54.380Z
2019-04-10T18:01:54.380Z
Jay
<p>So I want to edit date type filed on entry save, but I don't know how to format it. It times out on string and don't seem to accept DateTime objects either.</p>

<pre><code>Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent $event) {
        if ($event-&gt;element instanceof \craft\elements\Entry) {
            $entry = $event-&gt;element;
            if($entry-&gt;sectionId == 16){
                $entry-&gt;rangeStart = date_create_from_format('d/M/Y:H:i:s', '01/01/2019:19:00:00');
                Craft::$app-&gt;elements-&gt;saveElement($entry);
            }
        }
});
</code></pre>

<plugin-development><field><datetime>
How to edit a date field in a plugin
# How to edit a date field in a plugin

(Asked by: username_0 on 2019-04-10)

So I want to edit date type filed on entry save, but I don't know how to format it. It times out on string and don't seem to accept DateTime objects either.
Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent $event) {
        if ($event->element instanceof \craft\elements\Entry) {
            $entry = $event->element;
            if($entry->sectionId == 16){
                $entry->rangeStart = date_create_from_format('d/M/Y:H:i:s', '01/01/2019:19:00:00');
                Craft::$app->elements->saveElement($entry);
            }
        }
});

**Tags:** plugin-development, field, datetime


---

## 1 answers

### Answer by username_1 on 2019-04-10. Score: 1 | Accepted answer

The problem you have here is you are calling saveElement from EVENT_AFTER_SAVE_ELEMENT which is causing an infinite loop.
You need to set:
$entry->propagating = true;

Before calling the saveElement method.
Dates should be a PHP DateTime class, e.g:
$entry->rangeStart = new \DateTime('2019-04-10 19:00:00');

> **Comments:**
> **username_0**: Ahh I was so set on date that I didn't see the loop, however setting $entry->propagating = true; didn't help. I've switched to before save event instead and now it works.  

---

