We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

generic PHP DateTime use results in an attempt to find a Phalcon class called DateTime

I need to persist timestamps in my MySQL database that are accurate down to milliseconds. In order to get a string representation of the current system time that is accurate to that level, I need to use the PHP class DateTime. But when I try and create an object:

$ts = new DateTime();
$str = ts->format('Y-m-d H:i:s:u');

The logs complain about 'MyProject\Models\DateTime' not being found (I am using namespaces in this project and the object in question is a Model). Thoughts?



300
Accepted
answer

try setting base namespace on datetime like:

$ts = new \DateTime();
$str = ts->format('Y-m-d H:i:s:u');


40.7k

Yup, that worked, thanks!

You are welcome. If my answer helped you maybe you could mark it as correct answer