Getting current date, time, and day in 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?
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');
Php has a date function which works very well. With laravel and blade you can use this without ugly <?php
echo tags. For example, I use the following in a .blade.php
file...
Copyright © {{ date('Y') }}
... and Laravel/blade translates that to the current year. If you want date time and day, you'll use something like this:
{{ date('Y-m-d H:i:s') }}
If you want to use datetime
class do:
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
The documentation for reference.
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.
{{ now()->toDateTimeString('Y-m-d') }}
For more information check doc