<p>Ok, so the answer was quite simple: I was using the PHP DateTime object instead of Crafts DateTime.</p>

<p>Removed the <code>\</code> on this line:</p>

<pre><code>$date = new \DateTime($article['article_start'], new \DateTimeZone('Europe/Oslo'));
</code></pre>

<p>and everything worked perfectly.</p>

CC BY-SA 3.0
2016-03-21T13:09:22.277Z
2016-03-21T13:09:22.277Z
megatrond
<p>I'm doing an import, and saving entries into a channel with <code>saveEntry()</code>, but the entry's URL gets saved in the wrong format.</p>

<p>The URL format for the channel is: <code>news/{postDate.year}/{slug}</code> and I'm saving the entry with the following code:</p>

<pre><code>$title = utf8_encode($article['article_language_name']);
$description = utf8_encode($article['article_description']);
$text = utf8_encode($article['article_text']);
$date = new \DateTime($article['article_start'], new \DateTimeZone('Europe/Oslo'));

$entry = new EntryModel();
$entry-&gt;sectionId = $targetSectionId;
$entry-&gt;typeId = $targetEntryType;
$entry-&gt;authorId = $targetAuthorId;
$entry-&gt;postDate = $date;
$entry-&gt;enabled = true;

$entry-&gt;getContent()-&gt;title = $title;
$entry-&gt;setContentFromPost(array(
    'leadText' =&gt; $description,
    'bodyText' =&gt; $text
));

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

<p>All the URIs lack the <code>year</code> param in them, but that does get included if I save the entry via the CP afterwards.</p>

<p>What am I doing wrong? The <code>postDate</code> seem to be in the correct format, but maybe I'm missing something?</p>

<plugin-development><entries><plugin-import>
Entry URIs incorrect when using entriesService->saveEntry()
# Entry URIs incorrect when using entriesService->saveEntry()

(Asked by: username_0 on 2016-03-21)

I'm doing an import, and saving entries into a channel with saveEntry(), but the entry's URL gets saved in the wrong format.
The URL format for the channel is: news/{postDate.year}/{slug} and I'm saving the entry with the following code:
$title = utf8_encode($article['article_language_name']);
$description = utf8_encode($article['article_description']);
$text = utf8_encode($article['article_text']);
$date = new \DateTime($article['article_start'], new \DateTimeZone('Europe/Oslo'));

$entry = new EntryModel();
$entry->sectionId = $targetSectionId;
$entry->typeId = $targetEntryType;
$entry->authorId = $targetAuthorId;
$entry->postDate = $date;
$entry->enabled = true;

$entry->getContent()->title = $title;
$entry->setContentFromPost(array(
    'leadText' => $description,
    'bodyText' => $text
));

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

All the URIs lack the year param in them, but that does get included if I save the entry via the CP afterwards.
What am I doing wrong? The postDate seem to be in the correct format, but maybe I'm missing something?

**Tags:** plugin-development, entries, plugin-import


---

## 1 answers

### Answer by username_0 on 2016-03-21. Score: 3 | Accepted answer

Ok, so the answer was quite simple: I was using the PHP DateTime object instead of Crafts DateTime.
Removed the \ on this line:
$date = new \DateTime($article['article_start'], new \DateTimeZone('Europe/Oslo'));

and everything worked perfectly.


---

