33

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

Thank you

Ben Racicot
  • 5,332
  • 12
  • 66
  • 130
Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130

17 Answers17

45

Create a timestamp for 00:00 on the first day of next month:

$firstDayNextMonth = strtotime('first day of next month');

The number of days til that date is the number of seconds between now and then divided by (24 * 60 * 60).

$daysTilNextMonth = ($firstDayNextMonth - time()) / (24 * 3600);
Benji XVI
  • 2,192
  • 1
  • 25
  • 24
  • i got 29.895185185185, when i try this – Utku Dalmaz Nov 22 '09 at 01:32
  • 1
    This is wrong during December. It will generate a time that is the 0th day of the 0th month of next year (which logically is the last day of November in the current year). It should be `$startDateRange = mktime(0, 0, 0, 1, 1, $curYear+1);` – Crashthatch Aug 16 '15 at 19:58
43
$firstDayNextMonth = date('Y-m-d', strtotime('first day of next month'));

For getting first day after two months from current

$firstDayAfterTwoMonths = date('Y-m-d', strtotime('first day of +2 month'));
Venkat Kotra
  • 10,413
  • 3
  • 49
  • 53
9

You can use DateTime object like this to find out the first day of next month like this:

$date = new DateTime('first day of next month');

You can do this to know how many days left from now to the first day of next month:

$date = new DateTime('now');
$nowTimestamp = $date->getTimestamp();
$date->modify('first day of next month');
$firstDayOfNextMonthTimestamp = $date->getTimestamp();
echo ($firstDayOfNextMonthTimestamp - $nowTimestamp) / 86400;
rydje
  • 115
  • 1
  • 7
5

The easiest and quickest way is to use strtotime() which recognizes 'first day next month';

$firstDayNextMonth = date('Y-m-d', strtotime('first day next month'));
Yada
  • 30,349
  • 24
  • 103
  • 144
  • 1
    This might break in some cases, p.e. trying this today (`2012-11-30`) results to `2012-12-31`. It might be safer to implement it like this: `$firstDayNextMonth = date('Y-m-01', strtotime('next month'));` – Bjoern Nov 30 '12 at 07:56
  • 4
    @Bjoern Well that happens when you write `'first day next month'`. It should be `'first day of next month'` and everything will work as expected. – Ronnie May 23 '14 at 08:21
  • Also works with following months: `strtotime('first day of +2 month')` – Peter Dec 30 '15 at 14:48
3

Since I googled this and came to this answer, I figured I'd include a more modern answer that works for PHP 5.3.0+.

//Your starting date as DateTime
$currentDate = new DateTime(date('Y-m-d'));

//Add 1 month
$currentDate->add(new DateInterval('P1M'));

//Get the first day of the next month as string
$firstDayOfNextMonth = $currentDate->format('Y-m-1');
  • This is incorrect! If this code runs on 2021-01-30 the result will incorrectly be 2021-03-01. Correct would be 2021-02-01. This fails to account for months having varying lengths. – jlh Feb 01 '21 at 11:46
1

$firstDayNextMonth = date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));

Alex
  • 11
  • 1
1

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!

mattbasta
  • 13,492
  • 9
  • 47
  • 68
1

As another poster has mentioned the DateInterval object does not give accurate results for the next month when you use dates such as 1/31/2016 or 8/31/2016 as it skips the next month. My solution was to still use the DateInterval object but reformat your current date to be the first day of the current month prior to utilizing the DateInterval.

$currentDate = '8/31/2016';
$date = new DateTime(date("n", strtotime($currentDate))."/1/".date("Y", strtotime($currentDate)));
//add 1 month
$date->add(new DateInterval('P1M'));
$currentDate=$date->format('m/1/Y'); 

echo($currentDate);
1

easiest way to get the last day of the month

date('Y-m-d', mktime(0, 0, 0, date('m')+1, 1, date('Y')));
Sourav Sarkar
  • 292
  • 3
  • 20
0

I took mattbasta's approach because it's able to get the 'first day of next month' with a given date, but there is a tiny problem in calculating the $nextmonth. The fix is as below:

$now = getdate();
$nextmonth = ($now['mon'] + 1) % 13 + 1;
$year = $now['year'];
if($nextmonth == 1)
    $year++;
else
    $nextmonth--;
$thefirst = gmmktime(0, 0, 0, $nextmonth, $year);
hailong
  • 1,409
  • 16
  • 19
0

I initially thought about using a DateInterval object (as discussed above in another answer) but it is not reliable. For example, if the current DateTime is 31 January and then we add on a month (to get the next month) then it will skip February completely!

Here is my solution:

function start_of_next_month(DateTime $datetime)
{
    $year = (int) $datetime->format('Y');
    $month = (int) $datetime->format('n');
    $month += 1;

    if ($month === 13)
    {
        $month = 1;
        $year += 1;
    }

    return new DateTime($year . '-' . $month . '-01');
}
Matt Kieran
  • 4,174
  • 1
  • 17
  • 17
0

Even easier way to get first and last day of next month

$first = strttotime("first day of next month");
$last = strttotime("last day of next month");
Community
  • 1
  • 1
  • 3
    Please elaborate more about yours answer. Posting just a piece of code is not enough. You can read more about good quality answers here: http://stackoverflow.com/help/how-to-answer – jakubbialkowski Dec 22 '16 at 13:50
  • Please explain your answer so your code is easier to understand – acostela Dec 22 '16 at 13:51
0

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.

0
$month = date('m')+1;
if ($month<10) {
    $month = '0'.$month;
}
echo date('Y-').$month.'-01';

Simplest way to achieve this. You can echo or store into variable.

Sufyan Shaik
  • 23
  • 1
  • 5
0

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

To get the first day of next month a clean solution:

<?php
$date = new DateTimeInmutable('now');
$date->modify('first day of next month');//here the magic occurs

echo $date->format('Y-m-d') . '\n';

Since you just want to calculate it I suggest using DateTimeInmutable class. using the class DateTime and $date->modify('first day of next month'); will modify your original date value.

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
  • You have some mistakes. The class name is [DateTimeImmutable](https://www.php.net/manual/en/class.datetimeimmutable.php) not `DateTimeInmutable`. Also, the fact it's immutable means you need to assign the modification to a second variable because the original `$date` variable won't be changed. – BadHorsie Apr 27 '21 at 16:04
-1

I came up with this for my needs:

if(date('m') == 12) { $next_month = 1; } else { $next_month = date('m')+1; }
if($next_month == 1) { $year_start = date('Y')+1; } else { $year_start = date('Y'); }
$date_start_of_next_month = $year_start . '-' . $next_month . '-01 00:00:00';

if($next_month == 12) { $month_after = 1; } else { $month_after = $next_month+1; }
if($month_after == 1) { $year_end = date('Y')+1; } else { $year_end = date('Y'); }
$date_start_of_month_after_next = $year_end . '-' . $month_after . '-01 00:00:00';

Please note that instead of getting $date_end_of_next_month I chose to go with a $date_start_of_month_after_next date, it avoids the hassles with leap years and months containing different number of days. You can simply use the >= comparaision sign for $date_start_of_next_month and the < one for $date_start_of_month_after_next.

If you prefer a timestamp format for the date, from there you will want to apply the strtotime() native function of PHP on these two variables.

Dharman
  • 30,962
  • 25
  • 85
  • 135
legibe
  • 552
  • 2
  • 6
  • 19
-4

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).

Elliot
  • 5,211
  • 10
  • 42
  • 70
  • 5
    You did nothing with this post other than explain how difficult it is to accomplish the goal. Next time, be more helpful or don't post at all. – mattbasta Nov 22 '09 at 01:21