2017-12-05 7 views
1
$createdTime = Carbon::parse($specialist->created_at) 
         ->diff(Carbon::now()) 
         ->format('%y yr, %m mo and %d days'); 

これは0 yr, 0 mo and 8 daysを返します。私は8 daysだけを返したい。0を返した場合は0を削除DateFormat

または1 yr, 0 mo and 8 days場合、1 yr and 8 days

+0

さて、あなたは何が0で何がないかをチェックし、それに応じてフォーマット文字列を動的に供給するか、後ですべての "0 abc"を削除する必要があります。 – CBroe

+1

'diffForHumans()'関数があなたにとって十分でない場合、条件付きで文字列を作成するだけです。 – Jeff

+0

@Jeff、diffForHumans()は8日前に返します。 ( – Steve

答えて

0

あり、むしろdiffForHumans()を使用するよりもよりよい解決策があるとstr_replace($time->diffForHumans(), ' ago')agoを削除を返します。

私はこのようなCarbonIntervalを使用してrecommand:diff()はあなたの期待どおりの結果を得るために、条件のチェックを実行することができDateIntervalインスタンスを返すので

$now = Carbon::now(); 
$years = $specialist->created_at->diffInYears($now); 
$months = $specialist->created_at->diffInDays($now->subYears($years)); 
$days = $specialist->created_at->diffInDays($now->subMonths($years)); 

// $hours = $specialist->created_at->diffInHours($now->subDays($days)); 
// $minutes = $specialist->created_at->diffInMinutes($with_duration->subHours($hours)); 
// $seconds = $specialist->created_at->diffInSeconds($with_duration->subMinutes($minutes)); 

return CarbonInterval::years($years)->months($months)->days($days)->forHumans(); 

//return CarbonInterval::years($years)->months($months)->days($days)->hours($hours)->minutes($minutes)->seconds($seconds)->forHumans(); 
関連する問題