Coder Perfect

In Laravel, getting the current date, time, and day


Using Laravel, I need to get the current date, time, and day.

$ldate = new DateTime(‘today’); $ldate = new DateTime(‘now’); $ldate = new DateTime(‘today’); $ldate = new DateTime(‘today’); $ldate = new DateTime(‘today’); $ldate

However, it always returns 1.

What is the best way to acquire the current date, time, and day?

Asked by AngularAngularAngular

Solution #1

Carbon dependence is a feature of Laravel.

Include the CarbonCarbon namespace if necessary when using Carbon::now().

Revise (usage and docs)

Let’s say I want to get the current date and time and save it as a string.

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

This will output in the standard Y-m-d H:i:s format, but there are a lot of pre-made formats, so you won’t have to struggle with PHP date time strings again with Carbon.


String formats for Carbon:

Answered by Everon

Solution #2

Try this,

$ldate = date('Y-m-d H:i:s');

Answered by Vinod VT

Solution #3

The date function in PHP is quite useful. You can use this without the ugly?php echo tags if you use Laravel and Blade. In a.blade.php file, for example, I use the following…

Copyright © {{ date('Y') }}

Laravel/blade then converts this to the current year. You’ll need something like this to get the date, time, and day:

{{ date('Y-m-d H:i:s') }}

Answered by Menasheh

Solution #4

If you want to use the datetime class, follow these steps:

$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');

For reference, here is the paperwork.

Answered by whyguy

Solution #5

The now() function in Laravel 5.5 can be used to get the current date and time.

To print a date in a blade file, write something like this.

{{  now()->toDateTimeString('Y-m-d') }}

Check out the document for further information.

Answered by Jigar

Post is based on