100

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?

Bert H
  • 1,087
  • 1
  • 15
  • 29
Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161

8 Answers8

236

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.

Ryan
  • 22,332
  • 31
  • 176
  • 357
Md Rashedul Hoque Bhuiyan
  • 10,151
  • 5
  • 30
  • 42
44

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;
Him Hah
  • 1,385
  • 14
  • 9
  • 2
    That returns a unix timestamp. Just `now()` will return in the typical `2018-10-05 17:55:08` format. – Ryan DuVal Oct 05 '18 at 21:55
  • 1
    @futzlarson `now()` actually returns a Carbon object. It'll be rendered as a string if you output it in, say, a Blade template, but it's fundamentally a full, complex object. – ceejayoz Feb 20 '19 at 14:17
  • True. For my purposes it's always rendered as a string. – Ryan DuVal Feb 20 '19 at 18:50
12

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.

Jerodev
  • 32,252
  • 11
  • 87
  • 108
  • How can I load carbon in composer? – Abrar Jahin Sep 22 '15 at 14:48
  • Normaly Laravel will add carbon automaticly in your `composer.json` file. But if you want to be sure, try installing it by typing `composer require nesbot/carbon` in your console in the root of your application. – Jerodev Sep 22 '15 at 14:52
11

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;
Sadee
  • 3,010
  • 35
  • 36
7

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.

Marcos Di Paolo
  • 527
  • 1
  • 5
  • 22
2

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.

0
date_default_timezone_set('Australia/Melbourne');   
$time = date("Y-m-d H:i:s", time()); 
4b0
  • 21,981
  • 30
  • 95
  • 142
  • 3
    While this code may solve the question, [including an explanation](//s.tk/meta/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – 4b0 Apr 03 '19 at 05:30
  • 1
    please pay attention that this code not relevant for Laravel framwork, this code will work but not help for this issue, and as @Shree wrote, adding explanations will be good. – shushu304 Apr 03 '19 at 07:36
0

The most proper way to make timestamp of carbon is as following:

use Carbon\Carbon
$now = Carbon::now()->getTimestamp(); // timestamp (seconds)
$now = Carbon::now()->getTimestampMs(); // timestamp (milliseconds)

Works in Laravel 5, 6, 7, 8, 9 and 10 (latest at the moment of writing). Fluent, readable, you don't need to remember all formats of php date.

Valentin Rusk
  • 630
  • 5
  • 13