xiaowang/kirby/config/fields/mixins/datetime.php

36 lines
660 B
PHP
Raw Normal View History

2021-10-29 18:05:46 +02:00
<?php
2022-03-22 15:39:39 +01:00
use Kirby\Toolkit\Date;
2021-10-29 18:05:46 +02:00
return [
2022-08-31 16:08:03 +02:00
'props' => [
/**
* Defines a custom format that is used when the field is saved
*/
'format' => function (string $format = null) {
return $format;
}
],
'methods' => [
'toDatetime' => function ($value, string $format = 'Y-m-d H:i:s') {
if ($date = Date::optional($value)) {
if ($this->step) {
$step = Date::stepConfig($this->step);
$date->round($step['unit'], $step['size']);
}
2022-03-22 15:39:39 +01:00
2022-08-31 16:08:03 +02:00
return $date->format($format);
}
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
return null;
}
],
'save' => function ($value) {
if ($date = Date::optional($value)) {
return $date->format($this->format);
}
2021-10-29 18:05:46 +02:00
2022-08-31 16:08:03 +02:00
return '';
},
2021-10-29 18:05:46 +02:00
];