Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Current Timestamp from Carbon in Laravel 5

I want to get current timestamp in laravel 5 and I have done this-

$current_time = Carbon\Carbon::now()->toDateTimeString(); 

I am getting eror- 'Carbon not found'-

enter image description here

What can I do?

Can anyone help, please?

like image 972
Abrar Jahin Avatar asked Sep 22 '15 14:09

Abrar Jahin


People also ask

What is Carbon :: now () in Laravel?

The purpose of this ( Carbon::now() ) is to return the current time with a Carbon instance. You can also use the laravel helper now() , so you won't need to import the Carbon class. And the purpose of Carbon is to make it easy to work with timestamps.


2 Answers

You can try this if you want date time string:

use Carbon\Carbon; $current_date_time = Carbon::now()->toDateTimeString(); // Produces something like "2019-03-11 12:25:00" 

If you want timestamp, you can try:

use Carbon\Carbon; $current_timestamp = Carbon::now()->timestamp; // Produces something like 1552296328 

See the official Carbon documentation here.

like image 107
Md Rashedul Hoque Bhuiyan Avatar answered Oct 02 '22 17:10

Md Rashedul Hoque Bhuiyan


For Laravel 5.5 or above just use the built in helper

$timestamp = now(); 

If you want a unix timestamp, you can also try this:

$unix_timestamp = now()->timestamp; 
like image 30
Him Hah Avatar answered Oct 02 '22 15:10

Him Hah