6

I have a small problem with dates in PHP.

When I made 31 + 1 month of January with this code

$newDate = date('Y-m-d', strtotime('31-01-2016'.' + 1 month'));
echo $newDate;

it gives me 2 March but I need given me 29 February,

I need to add 1 month and is not 30days.

ditto for all dates: for example 01 january + 1 month => 1 february

29 january + 1 month => 29 february

30 january + 1 month => 29 february

31 january + 1 month => 29 february

Thank for your help

4 Answers4

10

I think you are looking for this type of dates.

<?php
    $date = date('2016-01-31');
    $currentMonth = date("m",strtotime($date));
    $nextMonth = date("m",strtotime($date."+1 month"));
    if($currentMonth==$nextMonth-1 && (date("j",strtotime($date)) != date("t",strtotime($date)))){
        $nextDate = date('Y-m-d',strtotime($date." +1 month"));
    }else{
        $nextDate = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
    }
    echo "Next date would be : $nextDate";
?>

Check live demo : https://eval.in/610034

  1. If date is 31-01-2016 then next date would be 29-02-2016
  2. If date is 25-01-2016 then next date would be 25-02-2016
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
2

Simply try:

$date = new DateTime('2016-01-31');
$date->modify('last day of next month');

This of course only counts if you always go from the end of one moth to the end of the next one.

Barthy
  • 3,151
  • 1
  • 25
  • 42
  • 1
    I'd personally first do a `+1 month` and check if the new month is not `(previous+1)%12` and then do this. – apokryfos Jul 22 '16 at 11:17
  • @apokryfos you're right. It would be best then to use `next month`, wouldn't it? – Barthy Jul 22 '16 at 11:21
  • I don't think there's a single calculation to get OPs desired result because it is such a non-standard thing to do. – apokryfos Jul 22 '16 at 11:24
  • @apokryfos In C# there are methods `addDays, addMonths, addYears` build in the Framework. It's not 'such a non-standard thing'. The idea of 'next month' is VERY standard. My boss wants it, our customers want it. That's why I'm here. – Bitterblue Mar 10 '20 at 10:30
  • @Bitterblue getting 29 of February by adding 1 month to 31 of January is what it not standard here. – apokryfos Mar 13 '20 at 02:48
  • @apokryfos Well, ask you granma what should happen. Or better ask someone from the accounts department. Or even far more better, ask a solid and very successful framework just to have a more consistent software development throughout the world. .NET goes to 29. Fab. I just checked. – Bitterblue Mar 13 '20 at 05:57
  • @Bitterblue well, PHP doesn't do this. JavaScript's Date doesn't do this either, Java's Calendar has two methods, `add` and `roll` with `roll` doing this while `add` returning March. Where I'm standing it doesn't seem standard. – apokryfos Mar 13 '20 at 07:55
0

try this,

$date = "2016-01-29";
$date = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
echo $date;

https://3v4l.org/Y9PpV

Dave
  • 3,073
  • 7
  • 20
  • 33
0

How about something like this:

date_default_timezone_set('UTC');

$current_month = (int) date('m');
$year = date('y');
$newDate = date('Y-m-d', strtotime('31-1-2016'.' + 1 month'));

if($current_month == 12) 
{
    $new_month=0;
    $year++;
}

$d = new DateTime( $year.'-'.($current_month+1).'-01' ); 
echo $d->format( 'Y-m-t' )."\n";

Change $current_month / $year based on your needs......

GreensterRox
  • 6,432
  • 2
  • 27
  • 30