# Create page programmatically - set date

**URL:** <https://forum.getkirby.com/t/create-page-programmatically-set-date/13151>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 2, 2019, 9:00pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151 "2019-03-02T21:00:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gillesvauvarin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gillesvauvarin/32/2351_2.png) [@gillesvauvarin](https://forum.getkirby.com/u/gillesvauvarin)\
**Post date:** [March 2, 2019, 9:00pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/1 "2019-03-02T21:00:53Z")

</div>

I try to set a given date when creating a page programmatically but the current date/time is set instead.

Is it because I use `default: now` in my blueprints? Can I overwrite it when creating a page programmatically?

```auto
fields: 
  publication: 
      label: Publication date
      type: date
      time: true
      default: now

```

I create my page like this:

```auto
$publication_date = '1551744000'; // March, 05 th 2019
$content = [
			'title' => 'Your Ad title (3-5 words)',
			'description'	=> 'Your Ad description (300-350 characters)',
			'sponsor'	=> 'sponsor',
			'publication'	=> (int)$publication_date
		];
	
		// Generate an unique slug with Stripe transaction_id
		$slug = $event->data->object->payment_intent;

		// Create the "post" page
		kirby()->page('posts')->createChild([
			'content' => $content,
			'slug' => $slug,
			'template' => $template,
		]);

```

I pass the date as an (int) =\> timestamp, is it the good format?

Thx for you advices

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 2, 2019, 9:47pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/2 "2019-03-02T21:47:50Z")

</div>

When you create that page, what is stored in the content file before you open the page in the Panel? I don’t think it makes sense to store a Unix timestamp, given that the Panel stores a date in the format YYYY-mm-dd and probably doesn’t make sense of the timestamp (not tested).

---

<div class="post-metadata">

**Author:** ![gillesvauvarin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gillesvauvarin/32/2351_2.png) [@gillesvauvarin](https://forum.getkirby.com/u/gillesvauvarin)\
**Post date:** [March 2, 2019, 10:10pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/3 "2019-03-02T22:10:32Z")

</div>

> [@texnixe](#):
>
> YYYY-mm-dd

So you think I should rather set my date like this?

```auto
$content = [
	'title' => 'Your Ad title (3-5 words)',
	'description'	=> 'Your Ad description (300-350 characters)',
	'sponsor'	=> 'sponsor',
	'publication'	=> date( "Y-m-d" , (int)$publication_date );
	];

```

I give it a try …

---

<div class="post-metadata">

**Author:** ![gillesvauvarin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gillesvauvarin/32/2351_2.png) [@gillesvauvarin](https://forum.getkirby.com/u/gillesvauvarin)\
**Post date:** [March 2, 2019, 10:27pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/4 "2019-03-02T22:27:45Z")

</div>

Yep works with this format 🙂

Thank you @texnixe

---

<div class="post-metadata">

**Author:** ![lauri](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lauri/32/3305_2.png) [@lauri](https://forum.getkirby.com/u/lauri)\
**Post date:** [April 8, 2019, 3:55pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/5 "2019-04-08T15:55:25Z")

</div>

Not sure if this is a bug or I’m doing something wrong but… if I have this field in my blueprint:

```
image:
  type: files
  multiple: false
  label: Image
  width: 1/2

```

And I try to add an image programatically it doesn’t work. The field always stays empty.

```
$new = page('podcasts/category-a')->createChild([
'slug' => $slug,
'template' => 'episode',
'draft' => $draft,
'num' => $number,
'content' => [
	'title' => $item->get_title(),
	'date' => $item->get_date('d-m-Y'),
	'text' => $item->get_description(),
	'number'	=> (int) $number,
	'image' => $image,
]
]);

```

If I rename the image field in the php code to something else then the info does get saved. Are files supposed to be added somehow differently?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 8, 2019, 4:01pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/6 "2019-04-08T16:01:11Z")

</div>

If it works with another field name, then I assume it interferes with the native image method, but would have to check what happens in the source code.

---

<div class="post-metadata">

**Author:** ![thoryglory](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@thoryglory](https://forum.getkirby.com/u/thoryglory)\
**Post date:** [September 1, 2020, 3:07pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/7 "2020-09-01T15:07:32Z")

</div>

I also want to create a page programmatically and want to add an image from the assets. But I dont know what to write instead of your $image? What is behind this variable? How do I refer to an image from the assets?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 1, 2020, 5:46pm UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/8 "2020-09-01T17:46:36Z")

</div>

What exactly do you want to achieve? In the example, only a reference to an image is stored in a field, but not actually a file moved into the files folder.

If you want to store a link to a file, you can create an asset from a file in the assets folder using the `asset()` helper: [https://getkirby.com/docs/reference/templates/helpers/asset](https://getkirby.com/docs/reference/templates/helpers/asset), then get the url from that object.

But if you want to use a files field like in the example, it doesn’t accept a URL but needs a file object.

---

<div class="post-metadata">

**Author:** ![thoryglory](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@thoryglory](https://forum.getkirby.com/u/thoryglory)\
**Post date:** [September 2, 2020, 7:35am UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/9 "2020-09-02T07:35:35Z")

</div>

So under specific circumstances I want to create a child of a page programmatically – that is working well so far. But in this specific case I also want to pre-fill the text (what is working) and image fields.  
I want to do that, because the page should be online/visible as soon as the page is created and for that I need an image (because of the layout/design). At the moment the page is created but I first need to add an image via the panel. And I want to pre-fill it, and later you still can change the image via the panel.

```
<?php $downloadChild = $page->createChild([
	"slug" => "downloads",
	"template" => "downloads",
	"content" => [
		"title" => "Downloads",
		"pageIntro" => "text bla",
		"teaser_description" => "text text text"
		"teaser_image" => ??
	]
]); ?>

```

The text is easy. But how do I add there an image from the assets folder? I thought I could do it like this or so  
`"teaser_image" => "assets/img/image_name.jpg"`

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [September 2, 2020, 7:50am UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/10 "2020-09-02T07:50:33Z")

</div>

If you want to store something in a files field, the file must actually exist in the content folder, so you would have to move it there from the assets folder. Also, a files field expects that you store the data yaml-encoded.

The alternative would be to not store anything in that field, and only fall back to the image from the assets folder in your template. I think that’s probably the better option.

---

<div class="post-metadata">

**Author:** ![thoryglory](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@thoryglory](https://forum.getkirby.com/u/thoryglory)\
**Post date:** [September 2, 2020, 8:06am UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/11 "2020-09-02T08:06:32Z")

</div>

Ah ok, I kind of thought about that, but I also dont know how to add the file to that content folder. Do you have an example code or a reference to a tutorial? Sorry I am a total beginner in Kirby.

Its a special case, so I dont want to have that image as a default in the blueprint

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [September 2, 2020, 8:13am UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/12 "2020-09-02T08:13:29Z")

</div>

As I said, I wouldn’t even move the image into the folder that but handle the case that no image is present in the template.

---

<div class="post-metadata">

**Author:** ![thoryglory](https://avatars.discourse-cdn.com/v4/letter/t/b9e5f3/32.png) [@thoryglory](https://forum.getkirby.com/u/thoryglory)\
**Post date:** [September 2, 2020, 8:22am UTC](https://forum.getkirby.com/t/create-page-programmatically-set-date/13151/13 "2020-09-02T08:22:18Z")

</div>

Ah ok, I think I got it 🙂
