0

例如,我从用户那里获得了 25 号,如何使用 Carbon 或任何 PHP 助手生成完整的日期?

例如今天是 2015 年 4 月 22 日,那么它将返回 2015 年 4 月 25 日。

如果今天是 2015 年 4 月 26 日,那么它将返回 2015 年 5 月 25 日。

4

2 回答 2

4

这与@MarkBaker 的方法基本相同,但它使用的是 Carbon:

$next = 25;

$date = Carbon::today()->day($next);
if($date->isPast()){
    $date->month++;
}

echo $date;
于 2015-04-21T16:15:38.257 回答
1
$date = 25;

$now = new DateTime();
$then = clone $now;
$then->setDate($now->format('Y'), $now->format('m'), $date);
if ($then < $now) {
    $then->add(new DateInterval('P1M'));
}
echo $then->format('Y-m-d');
于 2015-04-21T16:04:17.197 回答