php 在 Laravel 中获取当前日期、时间、日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28109179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Current date, time , day in laravel
提问by AngularAngularAngular
I need to get the current date, time, day using laravel
我需要使用 Laravel 获取当前日期、时间、日期
I tried to echo $ldate = new DateTime('today');
and $ldate = new DateTime('now');
我试图回应 $ldate = new DateTime('today');
和$ldate = new DateTime('now');
But it is returning 1 always.
但它总是返回 1。
How can i get the current date, time , day in larvel
我如何在 larvel 中获取当前日期、时间、日期
回答by Everon
Laravel has the Carbon
dependency attached to it.
LaravelCarbon
附加了依赖项。
Carbon::now()
, include the Carbon\Carbon
namespace if necessary.
Carbon::now()
,Carbon\Carbon
如有必要,包括命名空间。
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.
这将以通常的格式输出Y-m-d H:i:s
,有许多预先创建的格式,您不太可能需要再次使用 Carbon 处理 PHP 日期时间字符串。
Documentation: https://github.com/briannesbitt/Carbon
文档:https: //github.com/briannesbitt/Carbon
String formats for Carbon: http://carbon.nesbot.com/docs/#api-formatting
Carbon 的字符串格式:http: //carbon.nesbot.com/docs/#api-formatting
回答by Vinod VT
Try this,
尝试这个,
$ldate = date('Y-m-d H:i:s');
回答by Menasheh
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...
PHP 有一个日期功能,效果很好。使用 laravel 和blade,你可以使用它而没有丑陋的<?php
回声标签。例如,我在.blade.php
文件中使用以下内容...
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:
... Laravel/blade 将其转换为当前年份。如果您想要日期时间和日期,您将使用以下内容:
{{ date('Y-m-d H:i:s') }}
回答by whyguy
If you want to use datetime class
如果你想使用 datetime class
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
回答by joy
回答by Amit Kumar
Here is another way to do this
这是执行此操作的另一种方法
Use \Carbon\Carbon;
$date = Carbon::now();
echo $date->toRfc850String();
$date = Carbon::now();
回声 $date->toRfc850String();
Output will be like this
输出将是这样的
Saturday, 11-May-19 06:28:04 UTC
回答by Soumitra Mandal
FOR LARAVEL 5.x
对于 Laravel 5.x
I think you were looking for this
我想你在找这个
$errorLog->timestamps = false;
$errorLog->created_at = date("Y-m-d H:i:s");
回答by f_i
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(), ];
回答by Sheetal Mehra
You can try this.
你可以试试这个。
use Carbon\Carbon;
$date = Carbon::now()->toDateTimeString();
回答by Roberto
You have a couple of helpers.
你有几个帮手。
The helper now() https://laravel.com/docs/7.x/helpers#method-now
助手 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() 有一个可选参数,即时区。所以你现在可以使用:
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.
以同样的方式,您可以使用 helper today() https://laravel.com/docs/7.x/helpers#method-today。这是 now() 的“同一件事”,但没有小时、分钟、秒。
At the end, under the hood they use Carbon as well.
最后,在引擎盖下,他们也使用碳。