如何以良好的格式显示剩余日期?

I am creating a value that shows me the remaining time from a date until now:

$date=date_create(date('Y-m-d H:i:s', strtotime($mydate)));
$curdate=date_create(date("Y-m-d H:i:s"));
$diff=date_diff($date,$curdate);
$diff1= $diff->format("%y years %m months %d days");

The result in this case is: 0 years 0 months 27 days

What I would like to do is, show only years or months if they are not 0. So in this case I would only like to get the result 27 days. And if it is only one day left, then it shows 1 days but it should show 1 day (so if the result is 1 show singular).

I tried something like this, but it didn't work:

$diff1= $diff->format(if("%y" !=0){"%y years"}); if("%m" !=0){"%m months"}) %d days");

date_diff returns an instance of the DateInterval class which has the class variables $y, $m and $d for the corresponding years, months and days.

As such what you were trying to achieve can still be done using these variables:

$diff=date_diff($date,$curdate);
$format = '';
if ($diff->y > 0) {
     $format = '%y years';
} else if ($diff->m > 0) {
     $format = '%m months';
} else {
     $format = '%d days';
}
$diff1 = $diff->format($format);

The elimination of the s in years/months/days is left up to the OP.

(edited)

In this case only one category will printed, but to get more than one at a time, modify the 'if-else' to individual 'if' and concatenation with previous categories,

if ($diff->y > 0) {
     $format = '%y years ';
}
if ($diff->m > 0) {
     $format .= '%m months ';
}
if ($diff->d > 0) {
     $format .= '%d days';
}

Powerful Function to get two date difference.

PARA: Date Should In YYYY-MM-DD Format

RESULT FORMAT: // '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds

// '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days

// '%m Month %d Day' => 3 Month 14 Day

// '%d Day %h Hours' => 14 Day 11 Hours

// '%d Day' => 14 Days

// '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds

// '%i Minute %s Seconds' => 49 Minute 36 Seconds

// '%h Hours => 11 Hours

// '%a Days => 468 Days

function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' ){
$datetime1 = date_create($date_1);

$datetime2 = date_create($date_2);

$interval = date_diff($datetime1, $datetime2);

return $interval->format($differenceFormat);}

Follow this link http://php.net/manual/en/function.date-diff.php