Skip to main content

DateSqlMapper

Formats a DateTimeInterface or n2n\util\calendar\Date value into a SQL date string (Y-m-d), or leaves null untouched. It performs no validation.

Accepts the following input types:

  • \DateTime or \DateTimeImmutable — formatted via DateUtils::dateToSql().
  • n2n\util\calendar\Date — formatted the same way.
  • null — left as null.

A value that is not a DateTimeInterface or Date (e.g. a string) throws BindMismatchException. Use Mappers::date() first if you need to parse strings.

Example: formatting to a SQL date string​

$result = Bind::values(new \DateTime('2010-01-01'), null)
->map(Mappers::dateSql())->toArray()->exec();
var_dump($result->get());

Output:

array(2) {
[0]=>
string(10) "2010-01-01"
[1]=>
NULL
}