<p><code>$timeslot</code> isn't formatted correctly to be used as the postDate. I'd create a field that takes a date and a time and then use <code>DateTimeHelper::formatTimeForDb()</code> to format for use as <code>postDate</code>.</p>

<p>You can't modify the <code>postDate</code> until after the entry has been saved - so will need to resave the entry with the newly set <code>postDate</code>. More info as to why you have to do this <a href="https://craftcms.stackexchange.com/questions/6722/%E2%80%A6">here</a>.</p>

<pre><code>craft()-&gt;on( 'entries.onSaveEntry', function( Event $event ){

  // Grab the entry from post request
  $entry = $event-&gt;params['entry'];

  // Get logged in user
  $user = craft()-&gt;userSession-&gt;getUser();

  if ( $user &amp;&amp; $user-&gt;admin ) {
    $timeSlot = $entry-&gt;getContent()-&gt;assignedTimeslot;

    // If the postDate and Timeslot don't match (stops infinite loop)
    if ( !( $entry-&gt;postDate == $timeSlot ) ) {

        // Set postDate to timeSlot date
        $entry-&gt;postDate = DateTimeHelper::formatTimeForDb( $timeSlot );

        // Resave entry
        craft()-&gt;entries-&gt;saveEntry( $entry );
    }
  }
});
</code></pre>

CC BY-SA 3.0
2016-05-17T14:37:39.000Z
the `$timeslot` variable is set to `2016-11-10 14:00:00` and if I `print_r` the `$entry->postDate` it returns an object `Craft\DateTime Object ( [date] => 2016-11-10 14:00:00.000000 [timezone_type] => 3 [timezone] => UTC )` So looks like it is formatted correctly. Any further clues?
Lettie
CC BY-SA 3.0
2016-05-17T15:35:09.607Z
So I've updated the logic with `DateTimeHelper::formatTimeForDb()` this ensures the formats match. I've tested locally and this is working for me. Give it a go.
Luke Pearce
CC BY-SA 3.0
2016-05-17T15:53:46.320Z
Not sure calling `saveEntry` inside of an `onSaveEntry` event is correct? The code hangs for me. Is that not setting up an infinite loop? Also the date is made up of 2 variables `assignedDate` and `assignedTimeslot` so you are only assigning the time to the `DateTimeHelper` without the date part.
Lettie
CC BY-SA 3.0
2016-05-17T16:35:23.160Z
Yes, that was a mistake. My suggestion was to combine your `assignedDate` field with you `assignedTimeslot` field. What fieldtype are these currently set up with? Unfortunately I don't think the postDate is accessible `onSaveEntry`. I've also tried with `beforeOnSaveEntry` and can set other custom field values but not the `postDate`.
Luke Pearce
CC BY-SA 3.0
2016-05-17T16:39:02.067Z
More info : http://craftcms.stackexchange.com/questions/6722/use-onbeforesaveentry-event-to-update-entrymodel-properties
Luke Pearce
CC BY-SA 3.0
2016-05-18T09:46:46.523Z
cheers Luke. It still didn't work for me as the code was still hanging trying to re-save the entry within the `onSaveEntry` but you got me on the right track. I re-jigged the code a little with the conditionals so it set the `postDate` check outside of the user check. I also created a new DateTime object as the check was failing. I guess it needs to check object against object. But now it works a treat. I will add final code as the answer. Once again thanks for your help.
Lettie
CC BY-SA 3.0
2016-05-18T09:49:53.040Z
Was it hanging when creating a new entry? Might need some more conditionals for `isNewEntry` or not.
Luke Pearce
CC BY-SA 3.0
2016-05-18T09:58:34.980Z
No on existing entries as well.
Lettie
CC BY-SA 3.0
2016-05-18T10:04:30.343Z
It would have worked if `assignedTimeslot` was a date/time field.
Luke Pearce
CC BY-SA 3.0
2016-05-17T13:07:18.703Z
2016-05-17T17:35:21.643Z
2017-04-13T12:50:59.930Z
Luke Pearce
<p>Thanks to @LukePearce I managed to re-write the code to work as follows:</p>

<pre><code>craft()-&gt;on( 'entries.onSaveEntry', function( Event $event ){

  // Grab the entry from post request
  $entry = $event-&gt;params['entry'];

  // Get logged in user
  $user = craft()-&gt;userSession-&gt;getUser();

  // Set the timeslot
  $timeSlot = $entry-&gt;getContent()-&gt;assignedDate.' '.$entry-&gt;getContent()-&gt;assignedTimeslot;

  // Create a new dateTime object from timeslot
  $postDate = new \DateTime($timeSlot);

  // Check the postDate has changed
  if ($entry-&gt;postDate != $postDate) {

    // check user is set and is admin
    if ($user &amp;&amp; $user-&gt;admin) {

      // Set entry postDate to new postDate
      $entry-&gt;postDate = $postDate;

      // Resave entry
      $success = craft()-&gt;entries-&gt;saveEntry($entry);

    }
   }
});
</code></pre>

CC BY-SA 3.0
2016-05-18T09:57:19.637Z
2016-05-18T09:57:19.637Z
Lettie
<p>I have written a plugin for this exact use case. You can configure what time is set as the postDate or expiryDate via a Twig template.</p>

<p><a href="https://github.com/carlcs/craft-autoexpire" rel="nofollow">https://github.com/carlcs/craft-autoexpire</a></p>

CC BY-SA 3.0
2016-05-19T04:13:14.837Z
2016-05-19T04:13:14.837Z
carlcs
<p>I think I'm almost there with this one but obviously missing something as the postDate is not updating. I have the following in an onSaveEntry event. It's saying it's successful but the postDate does not change. Ideally I want this to update whether the entry is enabled or not.</p>

<pre><code>public function onSaveEntry(Event $event)
{
    $entry = $event-&gt;params['entry'];
    // set postDate from assignedDate
    if($user-&gt;admin) {
        if($entry-&gt;assignedDate &amp;&amp; $entry-&gt;assignedTimeslot) {
            $timeslot = $entry-&gt;assignedDate.' '.$entry-&gt;assignedTimeslot.'.000000';
            $params = $entry-&gt;postDate = $timeslot;
            $success = craft()-&gt;elements-&gt;saveElement($entry, $params);
        }
    }
    return $event;
}
</code></pre>

CC BY-SA 3.0
2016-05-17T11:59:42.513Z
I would try using a single field on the entry called `assignedDateTime` which can take a date and a time. This can then be used to set the postDate. Why has the timeslot got `'.00000'` after it?
Luke Pearce
CC BY-SA 3.0
2016-05-17T14:23:05.807Z
@LukePearce just something I added after a bit of googling. But didn't work. Doesn't work when removed either.
Lettie
CC BY-SA 3.0
2016-05-17T14:39:40.077Z
@LukePearce sorry correction on that as well as Googling I picked it up from the object postDate returns.
Lettie
<plugins><postdate><onsaveentry>
Updating the postDate field from a plugin?
# Updating the postDate field from a plugin?

(Asked by: username_0 on 2016-05-17)

I think I'm almost there with this one but obviously missing something as the postDate is not updating. I have the following in an onSaveEntry event. It's saying it's successful but the postDate does not change. Ideally I want this to update whether the entry is enabled or not.
public function onSaveEntry(Event $event)
{
    $entry = $event->params['entry'];
    // set postDate from assignedDate
    if($user->admin) {
        if($entry->assignedDate && $entry->assignedTimeslot) {
            $timeslot = $entry->assignedDate.' '.$entry->assignedTimeslot.'.000000';
            $params = $entry->postDate = $timeslot;
            $success = craft()->elements->saveElement($entry, $params);
        }
    }
    return $event;
}

**Tags:** plugins, postdate, onsaveentry

> **Comments:**
> **username_1**: I would try using a single field on the entry called `assignedDateTime` which can take a date and a time. This can then be used to set the postDate. Why has the timeslot got `'.00000'` after it?  
> **username_0**: @LukePearce just something I added after a bit of googling. But didn't work. Doesn't work when removed either.  
> **username_0**: @LukePearce sorry correction on that as well as Googling I picked it up from the object postDate returns.  

---

## 3 answers

### Answer by username_1 on 2016-05-17. Score: 4

$timeslot isn't formatted correctly to be used as the postDate. I'd create a field that takes a date and a time and then use DateTimeHelper::formatTimeForDb() to format for use as postDate.
You can't modify the postDate until after the entry has been saved - so will need to resave the entry with the newly set postDate. More info as to why you have to do this here.
craft()->on( 'entries.onSaveEntry', function( Event $event ){

  // Grab the entry from post request
  $entry = $event->params['entry'];

  // Get logged in user
  $user = craft()->userSession->getUser();

  if ( $user && $user->admin ) {
    $timeSlot = $entry->getContent()->assignedTimeslot;

    // If the postDate and Timeslot don't match (stops infinite loop)
    if ( !( $entry->postDate == $timeSlot ) ) {

        // Set postDate to timeSlot date
        $entry->postDate = DateTimeHelper::formatTimeForDb( $timeSlot );

        // Resave entry
        craft()->entries->saveEntry( $entry );
    }
  }
});

> **Comments:**
> **username_0**: the `$timeslot` variable is set to `2016-11-10 14:00:00` and if I `print_r` the `$entry->postDate` it returns an object `Craft\DateTime Object ( [date] => 2016-11-10 14:00:00.000000 [timezone_type] => 3 [timezone] => UTC )` So looks like it is formatted correctly. Any further clues?  
> **username_1**: So I've updated the logic with `DateTimeHelper::formatTimeForDb()` this ensures the formats match. I've tested locally and this is working for me. Give it a go.  
> **username_0**: Not sure calling `saveEntry` inside of an `onSaveEntry` event is correct? The code hangs for me. Is that not setting up an infinite loop? Also the date is made up of 2 variables `assignedDate` and `assignedTimeslot` so you are only assigning the time to the `DateTimeHelper` without the date part.  
> **username_1**: Yes, that was a mistake. My suggestion was to combine your `assignedDate` field with you `assignedTimeslot` field. What fieldtype are these currently set up with? Unfortunately I don't think the postDate is accessible `onSaveEntry`. I've also tried with `beforeOnSaveEntry` and can set other custom field values but not the `postDate`.  
> **username_1**: More info : http://craftcms.stackexchange.com/questions/6722/use-onbeforesaveentry-event-to-update-entrymodel-properties  
> **username_0**: cheers Luke. It still didn't work for me as the code was still hanging trying to re-save the entry within the `onSaveEntry` but you got me on the right track. I re-jigged the code a little with the conditionals so it set the `postDate` check outside of the user check. I also created a new DateTime object as the check was failing. I guess it needs to check object against object. But now it works a treat. I will add final code as the answer. Once again thanks for your help.  
> **username_1**: Was it hanging when creating a new entry? Might need some more conditionals for `isNewEntry` or not.  
> **username_0**: No on existing entries as well.  
> **username_1**: It would have worked if `assignedTimeslot` was a date/time field.  

---

### Answer by username_0 on 2016-05-18. Score: 2

Thanks to @LukePearce I managed to re-write the code to work as follows:
craft()->on( 'entries.onSaveEntry', function( Event $event ){

  // Grab the entry from post request
  $entry = $event->params['entry'];

  // Get logged in user
  $user = craft()->userSession->getUser();

  // Set the timeslot
  $timeSlot = $entry->getContent()->assignedDate.' '.$entry->getContent()->assignedTimeslot;

  // Create a new dateTime object from timeslot
  $postDate = new \DateTime($timeSlot);

  // Check the postDate has changed
  if ($entry->postDate != $postDate) {

    // check user is set and is admin
    if ($user && $user->admin) {

      // Set entry postDate to new postDate
      $entry->postDate = $postDate;

      // Resave entry
      $success = craft()->entries->saveEntry($entry);

    }
   }
});


---

### Answer by username_2 on 2016-05-19. Score: 2

I have written a plugin for this exact use case. You can configure what time is set as the postDate or expiryDate via a Twig template.

<https://github.com/username_2/craft-autoexpire>


---

