Decimoseptimo Decimoseptimo - 5 months ago 20
MySQL Question

PHP Datetime object outputs trailing zeroes, affects query against MySQL datetime

The following expression

$datetime = new \DateTime('2016-06-02 19:30:17', new \DateTimeZone('America/Tijuana'));
$datetime->setTimezone(new \DateTimeZone('GMT'));


is returning
2016-06-03 02:30:17.000000
(note that it is outputting 0's are not present in the input).

So in Yii2's query building, a query against a db datetime column like the following, cannot be made because of the trailing 0's.

->andFilterWhere(['created_at' => $this->datetime->format('php:Y-m-d H:i:s')]);


Why is datetime object outputting 0's in the first place?

I suppose a comparison with
BETWEEN
or operands like
>=
might do, any thoughts?

Answer

Those are the microseconds which are generated by default as the default format is Y-m-d H:i:s.u. You can override it by specifying Y-m-d H:i:s if that's what you want.