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?
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.
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.
$current_date_time = new DateTime("now"); $user_current_date = $current_date_time->format("Y-m-d"); to get toDay date.
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
Try this,
$ldate = date('Y-m-d H:i:s');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With