Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 641
Praveen Srinivasan Avatar asked Jun 08 '15 12:06

Praveen Srinivasan


People also ask

How does laravel store dates?

You can use Carbon\Carbon::parse($date); and then pass the Carbon object to eloquent model. I have date like '02 jul 2019' in My data input.

What is the use of Carbon in laravel?

The Carbon package can be used for many purposes, such as reading the current date and time, changing the default date and time format, finding the difference between two dates, converting the date and time from one timezone to another timezone, etc.


1 Answers

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(); 
like image 79
Limon Monte Avatar answered Sep 19 '22 20:09

Limon Monte