<p>The <code>postDate</code> attribute should be set to a <code>DateTime</code> instance. You might want to use Craft's <a href="https://docs.craftcms.com/api/v4/craft-helpers-datetimehelper.html" rel="nofollow noreferrer"><code>DateTimeHelper</code></a> class, i.e. something like</p>
<pre class="lang-php prettyprint-override"><code>$entry-&gt;postDate = \craft\helpers\DateTimeHelper::toDateTime(date(&quot;Y-m-d H:i&quot;, $date));
</code></pre>
<p>Note that in addition to the date the <a href="https://docs.craftcms.com/api/v4/craft-helpers-datetimehelper.html#public-methods" rel="nofollow noreferrer"><code>toDateTime()</code></a> method takes two additional parameters, <code>$assumeSystemTimeZone</code> and <code>$setToSystemTimeZone</code>, that you might have to play around with to get the time set correctly if the timestamp isn't UTC initially.</p>

CC BY-SA 4.0
2022-10-28T22:57:11.160Z
Thx for example. ))) But i use this code: $date = gmdate("Y-m-d H:i:s", 1666064471); $entry->postDate = new \DateTime($date); Is my decision normal? Or is it better to use your method?
Dimi
CC BY-SA 4.0
2022-10-28T08:16:28.570Z
2022-10-28T08:16:28.570Z
Mats Mikkel Rummelhoff
<p>please help me figure out how to add my entry to the section with my publication date? Here is my code:</p>
<pre><code>$date = 1666064471;

$entry = new Entry();
$entry-&gt;sectionId = 10;
$entry-&gt;enabled = true;
$entry-&gt;postDate = gmdate(&quot;Y-m-d H:i&quot;, $date);
$entry-&gt;title = &quot;Title entry&quot;;
$entry-&gt;setFieldValues([
      'reviewText' =&gt; $info,
]);
$success = Craft::$app-&gt;elements-&gt;saveElement($entry);
if (!$success) {
      Craft::error('Couldn’t save the entry &quot;'.$entry-&gt;title.'&quot;', __METHOD__);
}
</code></pre>
<p>If I understood correctly, then postDate is the system name of the publication date field. The same as $entry-&gt;title or $entry-&gt;enabled. postDate in my code is not a field I created.
if you remove this part of the code:</p>
<pre><code>$entry-&gt;postDate = gmdate(&quot;Y-m-d H:i&quot;, $date);
</code></pre>
<p>then everything is fine and the entry is added. But I need the entry added with this code not to have the current date.
Please help me figure out how to make this added entry date not the current one, but the one I specified. I don't want to create a separate field for this at all.</p>

<craft3>
How do I save my entry with my publication date?
# How do I save my entry with my publication date?

(Asked by: username_0 on 2022-10-27)

please help me figure out how to add my entry to the section with my publication date? Here is my code:
$date = 1666064471;

$entry = new Entry();
$entry->sectionId = 10;
$entry->enabled = true;
$entry->postDate = gmdate("Y-m-d H:i", $date);
$entry->title = "Title entry";
$entry->setFieldValues([
      'reviewText' => $info,
]);
$success = Craft::$app->elements->saveElement($entry);
if (!$success) {
      Craft::error('Couldn’t save the entry "'.$entry->title.'"', __METHOD__);
}

If I understood correctly, then postDate is the system name of the publication date field. The same as $entry->title or $entry->enabled. postDate in my code is not a field I created.
if you remove this part of the code:
$entry->postDate = gmdate("Y-m-d H:i", $date);

then everything is fine and the entry is added. But I need the entry added with this code not to have the current date.
Please help me figure out how to make this added entry date not the current one, but the one I specified. I don't want to create a separate field for this at all.

**Tags:** craft3

---

## 1 answers

### Answer by username_1 on 2022-10-28. Score: 0 | Accepted answer

The postDate attribute should be set to a DateTime instance. You might want to use Craft's DateTimeHelper class, i.e. something like
$entry->postDate = \craft\helpers\DateTimeHelper::toDateTime(date("Y-m-d H:i", $date));

Note that in addition to the date the toDateTime() method takes two additional parameters, $assumeSystemTimeZone and $setToSystemTimeZone, that you might have to play around with to get the time set correctly if the timestamp isn't UTC initially.

> **Comments:**
> **username_0**: Thx for example. ))) But i use this code: $date = gmdate("Y-m-d H:i:s", 1666064471); $entry->postDate = new \DateTime($date); Is my decision normal? Or is it better to use your method?  

---

