How to get Current Timestamp from Carbon in Laravel 5

PhpLaravelLaravel 5Php Carbon

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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;

Solution 3 - Php

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.

Solution 4 - Php

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;

Solution 5 - Php

With a \ before a Class declaration you are calling the root namespace:

$now = \Carbon\Carbon::now()->timestamp;

otherwise it looks for it at the current namespace declared at the beginning of the class. other solution is to use it:

use Carbon\Carbon
$now = Carbon::now()->timestamp;

you can even assign it an alias:

use Carbon\Carbon as Time;
$now = Time::now()->timestamp;

hope it helps.

Solution 6 - Php

It may be a little late, but you could use the helper function time() to get the current timestamp. I tried this function and it did the job, no need for classes :).

You can find this in the official documentation at https://laravel.com/docs/5.0/templates

Regards.

Solution 7 - Php

date_default_timezone_set('Australia/Melbourne');   
$time = date("Y-m-d H:i:s", time()); 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbrar JahinView Question on Stackoverflow
Solution 1 - PhpMd Rashedul Hoque BhuiyanView Answer on Stackoverflow
Solution 2 - PhpHim HahView Answer on Stackoverflow
Solution 3 - PhpJerodevView Answer on Stackoverflow
Solution 4 - PhpSadeeView Answer on Stackoverflow
Solution 5 - PhpMarcos Di PaoloView Answer on Stackoverflow
Solution 6 - PhpBrayhan E. Contreras ReyesView Answer on Stackoverflow
Solution 7 - PhpTereh SumasinoView Answer on Stackoverflow