composer require achse/nette-date-time-input
Basic usage is to add into your BaseForm or FormElements trait code like this:
/**
* @param string $name
* @param string|NULL $label
* @param IDateTimeConverter|string $dateConverterOrFormat
* @return DateTimeInput
*/
public function addDate($name, $label = NULL, $dateConverterOrFormat = 'j. n. Y')
{
return $this[$name] = DateTimeInputFactory::create($label, $dateConverterOrFormat);
}- You can use any
addRule(Form::XXXlike you are used to do for any other input. - Rules
Form::VALIDandForm::FILLEDare reimplemented to behave according to given DateTime format.
It creates simple text input. Handling client side is fully up to you. (For example: https://eonasdan.github.io/bootstrap-datetimepicker/)
As result it returns DateTime object. Internally it use:
IDateTimeConverter- responsible for conversion from string toDateTimeobject and vice versa.IDateTimeFixer- responsible for removing PHP specific behavior like this:
You can provide both of them as service. If not specified, single new object is created for each input.
In PHP, method DateTime::createFromFormat has this really unexpected behavior:
Therefore SimpleDateTimeConverter prevents you from being affected by this "language feature".
It works line this:
- It strips all duplicate whitespaces and trims the string,
DateTime::createFromFormatcreatesDateTimeobject,- object is formatted by original patter back to string,
- string is compared, if is same as input string.
(For more you can see: SimpleDateTimeConverter::parseValue method.)
But, there is a leading zero problem.
- You insert:
1. 1. 2015with patternd. m. Y, - algorithm creates object and ties to compare it with original,
- but by pattern created:
01. 01. 2015!==(original)1. 1. 2015.
Because of this, it's strongly recommended to use only no-leading-zero formats in your datepicker.
- If you wrote better (alternative)
IDateTimeConvertersend me pull request or just send me email. I'll be really happy to integrate it into package. - If you have ANY suggestion or idea how to make it better I'll be happy for every opened issue.


