Unexpected Behavior from dateFormatToSql
Unexpected Behavior from dateFormatToSql

in Editor
This is kind of an odd thing with the PHP library: dateSqlToFormat seems to handle datetimes, and I would expect dateFormatToSql to do that, too. It doesn’t. Example:
Field::inst( 'event.start' )
->validator( Validate::dateFormat( 'Y-m-d g:i A' ) )
->validator( Validate::required(
ValidateOptions::inst()
->message( 'Event start is required.' ) ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d g:i A' ) )
->setFormatter( Format::datetime('Y-m-d g:i A', 'Y-m-d H:i:s') ),
Field::inst( 'event.end' )
->validator( Validate::dateFormat( 'Y-m-d g:i A' ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d g:i A' ) )
->setFormatter( Format::dateFormatToSql( 'Y-m-d g:i A' ) ),
event.start will be set to the expected date time. event.end will be set to the date, but with the time component set to 12 AM.
Obviously, I can just use Format::datetime and everything is fine. It just seemed odd, and I thought I should mention it in case I am doing something wrong, or it needs to be fixed.
Replies
Hi,
Thanks for posting this. I considered the
dateSqlToFormat
anddateFormatToSql
functions to be for dates only - not dates + times. For that there is theFormat::datetime()
method as you mention.The reason that times aren't considered for
dateFormatToSql
is this line of code. Basically it always assumes that the input is an ISO8601 date only. It doesn't do any smart checking to see if the input as a time value or not.The fact that it works the other way around is actually not intentional - more an artifact of how it is implemented. In that case you get to specify the resulting format so the time component is retained, while going to ISO8601 the code specifies the format, so it is date only.
Allan