programing

Larabel 5의 카본에서 현재 타임스탬프를 얻는 방법

sourcejob 2022. 9. 22. 00:16
반응형

Larabel 5의 카본에서 현재 타임스탬프를 얻는 방법

현재 타임스탬프를 5라벨로 받고 싶어서 이렇게 했어요

$current_time = Carbon\Carbon::now()->toDateTimeString();

탄소를 찾을 수 없습니다.

여기에 이미지 설명 입력

내가 뭘 할 수 있을까?

누가 좀 도와줄래요?

날짜 시간 문자열을 원하는 경우 다음을 시도할 수 있습니다.

use Carbon\Carbon;
$current_date_time = Carbon::now()->toDateTimeString(); // Produces something like "2019-03-11 12:25:00"

타임스탬프가 필요한 경우 다음을 시도할 수 있습니다.

use Carbon\Carbon;
$current_timestamp = Carbon::now()->timestamp; // Produces something like 1552296328

Carbon 공식 문서를 참조하십시오.

Larabel 5.5 이상의 경우 내장된 도우미를 사용하십시오.

$timestamp = now();

UNIX 타임스탬프를 사용하는 경우는, 다음의 조작도 실행할 수 있습니다.

$unix_timestamp = now()->timestamp;

하나 더 추가해야 합니다.\루트 네임스페이스에서 시작하는 카본 클래스보다 먼저 표시됩니다.

$current_time = \Carbon\Carbon::now()->toDateTimeString();

또한 카본이 로딩되어 있는지 확인합니다.composer.json.

Larabel 5.2 <= 5.5

    use Carbon\Carbon; // You need to import Carbon
    $current_time = Carbon::now()->toDayDateTimeString(); // Wed, May 17, 2017 10:42 PM
    $current_timestamp = Carbon::now()->timestamp; // Unix timestamp 1495062127

또한 블레이드에서 특정 날짜 및 시간에 대한 datetime 형식을 변경하는 방법은 다음과 같습니다.

{{\Carbon\Carbon::parse($dateTime)->format('D, d M \'y, H:i')}}

Larabel 5.6 <

$current_timestamp = now()->timestamp;

를 사용하여\클래스 선언 전에 루트 이름 공간을 호출합니다.

$now = \Carbon\Carbon::now()->timestamp;

그렇지 않으면 클래스 시작 시 선언된 현재 네임스페이스에서 검색합니다.다른 솔루션은 다음과 같습니다.

use Carbon\Carbon
$now = Carbon::now()->timestamp;

에일리어스를 할당할 수도 있습니다.

use Carbon\Carbon as Time;
$now = Time::now()->timestamp;

도움이 됐으면 좋겠네요

조금 늦을 수도 있지만 도우미 함수 time()을 사용하여 현재 타임스탬프를 얻을 수 있습니다.이 기능을 사용해 보니, 작업이 완료되어 클래스가 필요 없습니다. :)

이것은, https://laravel.com/docs/5.0/templates 의 공식 메뉴얼에 기재되어 있습니다.

안부 전해요.

date_default_timezone_set('Australia/Melbourne');   
$time = date("Y-m-d H:i:s", time()); 

언급URL : https://stackoverflow.com/questions/32719972/how-to-get-current-timestamp-from-carbon-in-laravel-5

반응형