Creating page programatically, what to pass to date fields?

I am creating pages programatically:

$data = [
	'title'	=> $title,
	'photo_credits' => $photoCredits,
	'openingdate' => $openingDate,
	'closingdate' => $closingDate,
	'artists' => $artists,		
];

try {
	// authenticate as almighty
	$kirby->impersonate('kirby');

	page('exhibitions')->createChild([
		'slug'     => md5(str::slug($data['title'] . microtime())),
		'template' => 'exhibition-item',
		'content'  => $data
	]);
} catch (Exception $e) {
	//...
}

But the openingdate and closingdate fields are not being filled.

$openingDate and ·$closingDate are variables created via strtotime, so timestamp ints, afaik.

What am I supposed to pass to date fields in the content if not that?

Thanks

I think your date values should be of type string, so no need to cast them to date via strtotime? Instead, try to save that string directly.

1 Like

Theoretically, it doesn’t really matter what you store in the date fields, you can store a timestamp if that is what you want to store.

The Panel, however, will not display a unix timestamp in a type date field.

So the question is: Does nothing in fact get stored or is what is stored not displayed in the Panel. Also, to debug this, we would need to see the code for these variables rather than a description.

Thanks for answering.

The dates are originally an array that looks like this :

Array
(
    [0] => January 20th
    [1] => March 11th
    [2] => 2017
)

And I turn them into timestamps like this:

$openingDate = strtotime($dates[0] . (count($dates) == 4 ? $dates[1] : $dates[2]));
$closingDate = strtotime((count($dates) == 4 ? $dates[2] : $dates[1]) . (count($dates) == 4 ? $dates[3] : $dates[2]));

In the content file, the ‘openingdate’ and ‘closingdate’ fields show the default value (today).

----

Openingdate: 2020-09-18

----

Closingdate: 2020-09-18

----

The rest of variables/fields are correctly stored and displayed

Thanks

Storing the dates in the usual format does actually work:

	$data = [
		'title'	=> $title,
		'photo_credits' => $photoCredits,
		'openingdate' => date('Y-m-d',$openingDate),
		'closingdate' => date('Y-m-d',$closingDate),
		'artists' => $artists,		
	];
    ...
    ----

    Openingdate: 2017-01-20

    ----

    Closingdate: 2017-03-11

    ----

The problem arises if you want to store a timestamp in a field that is actually defined as a date field. A date field expects the format in the way the Panel can understand it, so not a timestamp. If you want to store a timestamp, don’t use a date field, but a text field.

1 Like

Always keep in mind that Kirby actually reads the blueprints and validates the content when you create a page programmatically, so the information must be in line with what you define in the blueprints, otherwise the content wouldn’t be readable in the Panel and not in line with your data model. That is also the reason why disabled fields are not writable via createChild() or create().

This will not happen if there is no blueprint, e.g. if you create content that must not be available for reading or editing in the Panel, or if you define fields that do not expect any particular content types.

1 Like