Getting current date, time, and day in Laravel
PhpDateLaravelPhp Problem Overview
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?
Php Solutions
Solution 1 - Php
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
Solution 2 - Php
Try this,
$ldate = date('Y-m-d H:i:s');
Solution 3 - Php
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') }}
Solution 4 - Php
If you want to use datetime
class do:
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
The documentation for reference.
Solution 5 - Php
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
Solution 6 - Php
Here is another way to do this
Use \Carbon\Carbon;
> $date = Carbon::now(); > > echo $date->toRfc850String();
Output will be like this
Saturday, 11-May-19 06:28:04 UTC
Solution 7 - Php
How about
$date = Carbon::now();
return $date->toArray();
will give you
{ "year": 2019, "month": 1, "day": 27, "dayOfWeek": 0, "dayOfYear": 26, "hour": 10, "minute": 28, "second": 55, "englishDayOfWeek": "Sunday", "micro": 967721, "timestamp": 1548570535, "formatted": "2019-01-27 10:28:55", "timezone": { "timezone_type": 3, "timezone": "Asia/Dubai" } }
The same props are accessible through
return [
'date' => $date->format('Y-m-d'),
'year' => $date->year,
'month' => $date->month,
'day' => $date->day,
'hour' => $date->hour,
'isSaturday' => $date->isSaturday(),
];
Solution 8 - Php
FOR LARAVEL 5.x
I think you were looking for this
$errorLog->timestamps = false;
$errorLog->created_at = date("Y-m-d H:i:s");
Solution 9 - Php
You can try this.
use Carbon\Carbon;
$date = Carbon::now()->toDateTimeString();
Solution 10 - Php
I prefer to use a built-in PHP function. if you want to get timestamp format such as "2021-03-31" you can write code like this
$date = date('Y-m-d', time());
for the time you can write like this
$date = date('H:i:s', time());
for the day you can write like this
$date = date('l', time()); // lowercase of L
function time()
will give you the current UNIX time and you convert it to whatever format you need.
So, you don't need any third-party package anymore :)
You can read more about UNIX time in this Wikipedia page and convert it in this webiste
Last, for the formatting, you can visit the w3schools page.
Solution 11 - Php
You can get any date-time format by following these rules.
$dt = Carbon::now();
var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString()
echo $dt->toDateString(); // 1975-12-25
echo $dt->toFormattedDateString(); // Dec 25, 1975
echo $dt->toTimeString(); // 14:15:16
echo $dt->toDateTimeString(); // 1975-12-25 14:15:16
echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
Solution 12 - Php
You have a couple of helpers.
The helper now() https://laravel.com/docs/7.x/helpers#method-now
The helper now() has an optional argument, the timezone. So you can use now:
now();
or
now("Europe/Rome");
In the same way you could use the helper today() https://laravel.com/docs/7.x/helpers#method-today. This is the "same thing" of now() but with no hours, minutes, seconds.
At the end, under the hood they use Carbon as well.
Solution 13 - Php
use DateTime;
$now = new DateTime();
Solution 14 - Php
It's very simple:
Carbon::now()->toDateString()
This will give you a perfectly formatted date string such as 2020-10-29.
In Laravel 5.5 and above you can use now()
as a global helper instead of Carbon::now()
, like this:
now()->toDateString()
Solution 15 - Php
Laravel Blade View:
> {{\Carbon\Carbon::now()->format('d-m-Y')}}
With timezone:
> {{\Carbon\Carbon::now("Asia/Tokyo")->format('d-m-Y')}}
Format available list: https://www.php.net/manual/en/datetime.format.php
Timezone available list: https://www.php.net/manual/en/timezones.php
Solution 16 - Php
I use now()
on laravel 8 to create a user
User::create([
'name'=>'admin',
'email'=>'[email protected]',
'email_verified_at'=>now(),
'password'=>bcrypt('123456'),
]);
Solution 17 - Php
data
return now()->toDateString();
Time
return now()->toTimeString();
Solution 18 - Php
//vanilla php
Class Date {
public static function date_added($time){
date_default_timezone_set('Africa/Lagos');//or choose your location
return date('l F Y g:i:s ',$time);
}
}
Solution 19 - Php
You can set the timezone on you AppServicesProvider in Provider Folder
public function boot()
{
Schema::defaultStringLength(191);
date_default_timezone_set('Africa/Lagos');
}
and then use Import Carbon\Carbon
and simply use Carbon::now()
//To get the current time, if you need to format it check out their documentation for more options based on your preferences enter link description here
Solution 20 - Php
If you need the date directly in a input value of your view this can help you: (myview .blade.php)
<input type="date" name="Date" value="{{date('Y-m-d', time())}}">
Solution 21 - Php
today data and time
return now::();
time
return now()->toTimeString();
Solution 22 - Php
You can use today()
function.
$today = today('Europe/London');
$dayOfYear = $today->dayOfYear;
$dayOfWeek = $today->dayOfWeek;
Solution 23 - Php
If you want date of today
use namespace
use Carbon\Carbon as time;
code ,
$mytime=time::now();
$date=$mytime->toRfc850String();
$today= substr($date, 0, strrpos($date, ","));
dd($today)
output ,
"Sunday"