Php Laravel Carbon从当前日期获取下一个特定日期

Php Laravel Carbon从当前日期获取下一个特定日期,php,laravel,datetime,php-carbon,Php,Laravel,Datetime,Php Carbon,将碳与laravel 5.6一起使用 我想写一个代码,给我下一次发生的日期从当前日期 例如,给出下一个5月31日的日期 情景1: 输入:$currentDate='01-30-2019';//MM-DD-YYYY格式 预期产出:$next31年5月='2019年5月31日' 场景2: 输入:$currentDate='07-04-2019';//MM-DD-YYYY格式 预期产出:$next31五月='05-31-2020' 更新: 我试过下面的代码,但不满意 <?php public f

将碳与laravel 5.6一起使用

我想写一个代码,给我下一次发生的日期从当前日期

例如,给出下一个5月31日的日期

情景1:
输入:$currentDate='01-30-2019';//MM-DD-YYYY格式
预期产出:$next31年5月='2019年5月31日'

场景2:
输入:$currentDate='07-04-2019';//MM-DD-YYYY格式
预期产出:$next31五月='05-31-2020'

更新:

我试过下面的代码,但不满意

<?php
public function nextOccurance()
{
    $now = Carbon::now();
    $month= $now->month;
    $year = $now->year;
    if($month > 6)
    {
         echo Carbon::createMidnightDate($year+1, 5, 31);
    }
    else
    {
        echo Carbon::createMidnightDate(null, 5, 31);
    }
    exit();
}
?>


先谢谢你

这就像是要过下一个生日。
class Test
{
    public static function getNextBirthday($date)
    {
        // set birthday from current year
        $date = Carbon::createFromFormat('m-d-Y', $date);
        $date->year(Carbon::now()->year);

        // diff from 31 may to now
        // its negative than add one year, otherwise use the current
        if (Carbon::now()->diffInDays($date, false) >= 0) {
            return $date->format('m-d-Y');
        }

        return $date->addYear()->format('m-d-Y');
    }
}

echo Test::getNextBirtday('05-31-1990');

碳为这类材料提供了一个良好且流畅的界面

您可以
lastOfMonth()
获取月份的最后一天。对于添加年份,您可以添加
addYear(1)


我希望这能帮助你们解决这个问题

    $event = Carbon::parse('31 May');

    if (Carbon::now() >= $event){
       $nextEvent  = $event->addYear();
    } else {
       $nextEvent  = $event;
    }

    echo $nextEvent->format('m-d-Y');

你试了什么?你看过手册了吗?$now->addYear();公平地使用手册:它不像$now->addYear那么简单。。。
public function nextOccurance()
{
    // the 31th of May of the current year
    $day = Carbon::createFromFormat('m-d', '05-31');
    $now = Carbon::now();
    // If today after $day
    if($now >= $day) {
       // Gat a next year
       $day->modify('next year');
    }

    echo $day->format('Y-m-d');
    exit();
}
    $event = Carbon::parse('31 May');

    if (Carbon::now() >= $event){
       $nextEvent  = $event->addYear();
    } else {
       $nextEvent  = $event;
    }

    echo $nextEvent->format('m-d-Y');