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

前端 未结 15 774
难免孤独
难免孤独 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 15:12

    You can use the php date method to find the current month and date, and then you would need to have a short list to find how many days in that month and subtract (leap year would require extra work).

    0 讨论(0)
  • 2020-12-08 15:15

    easiest way to get the last day of the month

    date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));
    
    0 讨论(0)
  • 2020-12-08 15:17

    You can get the first of the next month with this:

    $now = getdate();
    $nextmonth = ($now['mon'] + 1) % 13 + 1;
    $year = $now['year'];
    if($nextmonth == 1)
        $year++;
    $thefirst = gmmktime(0, 0, 0, $nextmonth, $year);
    

    With this example, $thefirst will be the UNIX timestamp for the first of the next month. Use date to format it to your liking.

    This will give you the remaining days in the month:

    $now = getdate();
    $months = array(
        31,
        28 + ($now['year'] % 4 == 0 ? 1 : 0), // Support for leap years!
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31
    );
    $days = $months[$now['mon'] - 1];
    $daysleft = $days - $now['mday'];
    

    The number of days left will be stored in $daysleft.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题