I want the nearest date passed from given date using carbon
$givenDate = "2020-07-18";
Today is = 2020-12-01
So the result should be the 18th last month. $result = "2020-11-18"
you can use subMonth() from now to get the last month then create a new datetime with suitable values:
$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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.