Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Current date, time , day in laravel

Tags:

date

php

laravel

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

I tried to echo $ldate = new DateTime('today'); and $ldate = new DateTime('now');

But it is always returning 1.

How can I get the current date, time, and day?

like image 605
AngularAngularAngular Avatar asked Jan 23 '15 11:01

AngularAngularAngular


People also ask

How can I get current time in laravel?

From Laravel 5.5 you can use now() function to get the current date and time. In blade file, you can write like this to print date.

How can I get current date in laravel 8?

Using Carbon to get the current date and time php use Carbon\Carbon; Then you will be able to use all of the handy methods that Carbon provides. In some cases, you might want to output the current date directly in your Blade template.

How do you find the current date using Carbon?

$current_date_time = new DateTime("now"); $user_current_date = $current_date_time->format("Y-m-d"); to get toDay date.


2 Answers

Laravel has the Carbon dependency attached to it.

Carbon::now(), include the Carbon\Carbon namespace if necessary.

Edit (usage and docs)

Say I want to retrieve the date and time and output it as a string.

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

This will output in the usual format of Y-m-d H:i:s, there are many pre-created formats and you will unlikely need to mess with PHP date time strings again with Carbon.

Documentation: https://github.com/briannesbitt/Carbon

String formats for Carbon: http://carbon.nesbot.com/docs/#api-formatting

like image 187
Everon Avatar answered Oct 02 '22 21:10

Everon


Try this,

$ldate = date('Y-m-d H:i:s');
like image 37
Vinod VT Avatar answered Oct 02 '22 21:10

Vinod VT