Laravel에서 현재 날짜, 시간, 요일 가져 오기
laravel을 사용하여 현재 날짜, 시간, 요일을 가져와야합니다.
나는 에코를 시도 $ldate = new DateTime('today');
했고$ldate = new DateTime('now');
그러나 항상 1을 반환합니다.
Larvel에서 현재 날짜, 시간, 요일을 어떻게 얻을 수 있습니까?
Laravel에는 Carbon
종속성이 첨부되어 있습니다.
Carbon::now()
, Carbon\Carbon
필요한 경우 네임 스페이스를 포함합니다 .
편집 (사용 및 문서)
날짜와 시간을 검색하여 문자열로 출력하고 싶다고 가정 해 보겠습니다.
$mytime = Carbon\Carbon::now();
echo $mytime->toDateTimeString();
이것은 일반적인 형식으로 출력되며 Y-m-d H:i:s
미리 만들어진 형식이 많이 있으며 Carbon으로 PHP 날짜 시간 문자열을 다시 엉망으로 만들 필요가 없을 것입니다.
문서 : https://github.com/briannesbitt/Carbon
Carbon의 문자열 형식 : http://carbon.nesbot.com/docs/#api-formatting
이 시도,
$ldate = date('Y-m-d H:i:s');
Php에는 매우 잘 작동하는 날짜 기능이 있습니다. laravel 및 blade를 사용하면 추악한 <?php
에코 태그 없이 이것을 사용할 수 있습니다 . 예를 들어 .blade.php
파일 에서 다음을 사용 합니다.
Copyright © {{ date('Y') }}
... 그리고 Laravel / blade는 그것을 올해로 번역합니다. 날짜 시간과 요일을 원하면 다음과 같이 사용합니다.
{{ date('Y-m-d H:i:s') }}
사용하려는 경우 datetime class
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s');
여기에 다른 방법이 있습니다.
Use \Carbon\Carbon;
$ date = Carbon :: now ();
echo $ date-> toRfc850String ();
출력은 다음과 같습니다.
Saturday, 11-May-19 06:28:04 UTC
Laravel 5.5 이후에는 now () 함수를 사용하여 현재 날짜와 시간을 가져올 수 있습니다.
블레이드 파일에 다음과 같이 작성하여 날짜를 인쇄 할 수 있습니다.
{{ now()->toDateTimeString('Y-m-d') }}
이것을 시도 할 수 있습니다.
use Carbon\Carbon;
$date = Carbon::now()->toDateTimeString();
use DateTime;
$now = new DateTime();
어때
$date = Carbon::now();
return $date->toArray();
너에게 줄 것이다
{ "연도": 2019, "월": 1, "일": 27, "dayOfWeek": 0, "dayOfYear": 26, "시간": 10, "분": 28, "초": 55, "englishDayOfWeek": "일요일", "마이크로": 967721, "타임 스탬프": 1548570535, "formatted": "2019-01-27 10:28:55", "시간대": { "timezone_type": 3, "timezone": "아시아 / 두바이" } }
동일한 소품은
반환 [ '날짜'=> $ date-> format ( 'Ym-d'), '연도'=> $ date-> year, '월'=> $ 날짜-> 월, 'day'=> $ date-> day, '시간'=> $ date-> 시간, 'isSaturday'=> $ date-> isSaturday (), ];
LARAVEL 5.x 용
이걸 찾고 있었던 것 같아요
$errorLog->timestamps = false;
$errorLog->created_at = date("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);
}
}
공급자 폴더에서 AppServicesProvider의 시간대를 설정할 수 있습니다.
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
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
참고 URL : https://stackoverflow.com/questions/28109179/getting-current-date-time-day-in-laravel
'code' 카테고리의 다른 글
'Microsoft.ACE.OLEDB.16.0'공급자가 로컬 컴퓨터에 등록되지 않았습니다. (0) | 2020.10.07 |
---|---|
파이썬의 문자열에서 인쇄 할 수없는 문자 제거 (0) | 2020.10.07 |
Windows 클립 보드에 복사하는 cygwin 명령 (0) | 2020.10.06 |
(사용되지 않음) onOptionsItemSelected 조각이 호출되지 않음 (0) | 2020.10.06 |
신속하게 BODY로 POST 요청을 보내는 방법 (0) | 2020.10.06 |