我想要距离给定日期最近的日期,使用复写纸
$givenDate = "2020-07-18";
今天是= 2020-12-01
所以结果应该是上个月的18号。$result = "2020-11-18"
发布于 2020-12-01 08:09:36
您可以从现在开始使用subMonth()来获取上个月,然后使用合适的值创建一个新的datetime:
$givenDate =Carbon::parse( "2020-12-20");
if($givenDate>Carbon::now())
$result=Carbon::create(Carbon::now()->year,Carbon::now()->month,$givenDate->day)
->format('yy-m-d');
else
$result=Carbon::create(Carbon::now()->year,Carbon::now()->subMonth()->month,$givenDate->day)
->format('yy-m-d');
https://stackoverflow.com/questions/65086630
复制相似问题