[php] 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?

This question is related to php laravel laravel-5 php-carbon

The answer is


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.


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.


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

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;

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.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-5

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory Can't install laravel installer via composer Including a css file in a blade template? No Application Encryption Key Has Been Specified How to Install Font Awesome in Laravel Mix Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes Laravel 5.4 redirection to custom url after login How to set the default value of an attribute on a Laravel model laravel 5.3 new Auth::routes()

Examples related to php-carbon

Laravel Carbon subtract days from current date Laravel 5 Carbon format datetime How to get Current Timestamp from Carbon in Laravel 5 Convert String to Carbon How to compare two Carbon Timestamps?