如何用四种不同的条件简化 if else

How to simplify if else with four different conditions

如何简化我的这段代码?

您可以将 Carbon::*() 的结果保存到变量中并调用其成员。

if (empty($startDate)) {
    $carbon = Carbon::now();
} else {
    $carbon = Carbon::createFromFormat(...);
}
$post->start_at = $carbon;
if (empty($endDate)) {
    $post->end_at = $carbon->endOfDay(); 
} else {
    $post->end_at = Carbon::createFromFormat(...);
}