Skip to main content

DateTimeSqlMapper

Formats a DateTimeInterface value into a SQL datetime string (Y-m-d H:i:s), or leaves null untouched. It performs no validation.

Accepts the following input types:

  • \DateTime or \DateTimeImmutable — formatted via DateUtils::dateTimeToSql().
  • null — left as null.

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

Example: formatting to a SQL datetime string​

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

Output:

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