How to get Current Timestamp from Carbon in Laravel 5
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.
You need to add another \
before your carbon class to start in the root namespace.
$current_time = \Carbon\Carbon::now()->toDateTimeString();
Also, make sure Carbon is loaded in your composer.json
.
Laravel 5.2 <= 5.5
use Carbon\Carbon; // You need to import Carbon
$current_time = Carbon::now()->toDayDateTimeString(); // Wed, May 17, 2017 10:42 PM
$current_timestamp = Carbon::now()->timestamp; // Unix timestamp 1495062127
In addition, this is how to change datetime format for given date & time, in blade:
{{\Carbon\Carbon::parse($dateTime)->format('D, d M \'y, H:i')}}
Laravel 5.6 <
$current_timestamp = now()->timestamp;
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;