如何在Laravel中获取当前时间和日期?
我需要利用Laravel得到现在的日期和日期。
我尝试用 $ldate = new DateTime('today');和$ldate = new DateTime('now');,但它总是返回1。
怎样才能得到正确的时间、日期?
我需要使用laravel获取当前日期,时间,日期 p>
我试图回显 如何在larvel中获取当前日期,时间,日期 p>
div> $ ldate = new DateTime('today'); code>和
$ ldate = new DateTime('now'); code> p>
但是它总是返回1。 p>
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 want to use datetime class
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
//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);
}
}
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') }}
use DateTime;
$now = new DateTime();
FOR LARAVEL 5.x
I think you were looking for this
$errorLog->timestamps = false;
$errorLog->created_at = date("Y-m-d H:i:s");
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
You can try this.
use Carbon\Carbon;
$date = Carbon::now()->toDateTimeString();
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(), ];
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
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