How to find first day of the next month and remaining days till this date with PHP

前端 未结 15 778
难免孤独
难免孤独 2020-12-08 14:36

How can I find first day of the next month and the remaining days till this day from the present day?

Thank you

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 14:53

    You could do something like this. To have this functionality, you need to make use of a php library available in https://packagist.org/packages/ishworkh/navigable-date.

    With that is really easy to do what you're asking for here. For e.g:

    $format = 'Y-m-d H:i:s';
    
    $Date = \NavigableDate\NavigableDateFacade::create('2017-02-24');
    var_dump($Date->format($format));
    
    $resetTime = true;
    $resetDays = true;
    
    $NextMonth = $Date->nextMonth($resetTime, $resetDays);
    
    var_dump($NextMonth->format($format));
    
    $DayUntilFirstOfNextMonth = $NextMonth->getDifference($Date);
    
    var_dump('Days left:' . $DayUntilFirstOfNextMonth->format('%d'));
    

    gives you ouput:

    string(19) "2017-02-24 00:00:00"
    string(19) "2017-03-01 00:00:00"
    string(11) "Days left:5"
    

    Note: Additionally this library let you navigate through dates by day(s), week(s), year(s) forward or backward. For more information look into its README.

提交回复
热议问题