how to use php DateTime() function in Laravel 5

I am using laravel 5. I have try to use the

$now = DateTime();
$timestamp = $now->getTimestamp(); 

But it shows error likes this.

 FatalErrorException in ProjectsController.php line 70:
 Call to undefined function App\Http\Controllers\DateTime()

What Can I do?


DateTime is not a function, but the class.

When you just reference a class like new DateTime() PHP searches for the class in your current namespace. However the DateTime class obviously doesn't exists in your controllers namespace but rather in root namespace.

You can either reference it in the root namespace by prepending a backslash:

$now = new \DateTime();

Or add an import statement at the top:

use DateTime;

$now = new DateTime();

Best way is to use the Carbon dependency.

With Carbon\Carbon::now(); you get the current Datetime.

With Carbon you can do like enything with the DateTime. Event things like this:

$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();