删除0,如果返回0年或0月DateFormat

时间:2017-12-05 15:46:30

标签: php laravel datetime datetime-format php-carbon

$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

或者如果1 yr, 0 mo and 8 days,请返回1 yr and 8 days

2 个答案:

答案 0 :(得分:1)

由于@IBAction func didSelect(_ control: UISegmentedControl) { switch control.selectedSegmentIndex { case 0: print ("1") case 1: // SHOWING THE PROFILE VIEW print ("2") default: print ("break") } } 返回diff()个实例,您可以运行条件检查以获得预期结果。

DateInterval

答案 1 :(得分:0)

有一个更好的解决方案,而不是diffForHumans()使用ago移除str_replace($time->diffForHumans(), ' ago')

我建议使用这样的CarbonInterval

$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();