找不到DateTime类,即使添加反斜杠也是如此

I'm trying to use DateTime to measure the time it takes for a part of a program to run.

My code is in the namespace 'App'. From my understanding, DateTime is in the global namespace, so it has to be prefixed with a blacklash. Which I've done, but I'm still getting an error that DateTime class can't be found.

$startTime= new \DateTime(date('H:i:s'));
//other code in here
$endTime = new \DateTime(date('H:i:s'));
$timeTaken = date_diff($startTime, $endTime)->format('%H:%i:%s');

When I run this I get the following error:

 Class 'App\DateTime' not found

Any ideas how I can resolve this?

in the begining of you php file add

use DateTime;

In your code

$startTime= new DateTime(date('H:i:s'));