2

For some reason, I cannot get strtotime('+1 month) to work. Here is my code;

$Date   = $_REQUEST['date']; //This is a unix time stamp
$Start  = $_REQUEST['start']; //This is a unix time stamp
$End    = $_REQUEST['end']; //This is a unix time stamp

to add a month onto my dates;

$monStart =strtotime('+1 month', $Start);
$monEnd   =strtotime('+1 month', $End);
$monDate  =strtotime('+1 month', $Date);

then to show my changed dates;

$vEnd = date('m/d/Y', $monEnd);
$vStart = date('m/d/Y', $monStart);
$vDate = date('m/d/Y', $monDate);

The problem that I have is that the supplied dates;

$Date = 1/31/2013
$Start = 1/01/2013
$End = 1/31/2013

Return;

$vDate = 3/03/2013
$vStart = 2/01/2013 //Only correct one
$vEnd = 3/03/2013

Please can someone help me?

ipfreelly
  • 21
  • 2
  • What are the expected values? – Otaia Apr 16 '13 at 17:48
  • $vDate = 2/28/2013, $VStart is corrtct, and $vEnd = 2/28/2013 – ipfreelly Apr 16 '13 at 17:51
  • @ipfreelly what's the issue. What output do you want – Sabari Apr 16 '13 at 17:51
  • @Sabari, I'm trying to get the next month, this is for an invoice. So the invoice's date is 1/31/2013, the start of the invoice is 1/1/2013 and the end is 1/31/2013. I added a button to increase the dates by one month but you can see the results that I am getting. – ipfreelly Apr 16 '13 at 17:57
  • If you're sure that the end date is the end of the month, you might be better off just iterating over the end-dates of the month, rather than trying to add one month to the date. – Cookyt Apr 16 '13 at 18:00
  • The problem is that not all invoices fall on the end of the month, they can also fall on the second to the last day of the month – ipfreelly Apr 16 '13 at 18:02
  • PHP thinks '1/31/2013 + 1 month' is 2/31/2013, which rolls over to Mar 3. If you need the last day of the month, you can use 'last day +1 month'. If it needs to be the same day but shifted, see here: http://stackoverflow.com/a/2557146/1955291 – Otaia Apr 16 '13 at 18:05
  • @ipfreelly check my answer, Hope that helps you – Sabari Apr 16 '13 at 18:14
  • I'll work on it in the morning, got to take take care of a crying new born, thanks everyone!!!!! – ipfreelly Apr 16 '13 at 18:21

2 Answers2

2

It's jumping to March because today is 31sth Jan, and adding a month gives 31st Feb, which doesn't exist, so it's moving to the next valid date. This is a PHP bug. You can get more info on that at https://bugs.php.net/bug.php?id=44073

You can try with DateTime to over come this scenario. You can use this function for your requirement

function add_month($date_value, $months, $format = 'm/d/Y') {
    $date = new DateTime($date_value);
    $start_day = $date->format('j');

    $date->modify("+{$months} month");
    $end_day = $date->format('j');

    if ($start_day != $end_day)
        $date->modify('last day of last month');

    return $date->format($format);
}

Now you can call :

$vEnd = add_month($monEnd, 1);
$vStart = add_month($monStart, 1);
$vDate = add_month($monDate, 1); 

This will give you :

$vDate = '02/28/2013';
$vStart = '02/01/2013';
$vEnd = '02/28/2013';

Hope this helps you :)

Sabari
  • 6,205
  • 1
  • 27
  • 36
1

DateTime is much better for handling date math as it account for things like days in the month:

$dt = new DateTime('2013-02-01');
$dt->modify('+1 month');
echo $dt->format('Y-m-d');

See it in action

Since you're using timestamps it might look like this:

$dt = new DateTime('@'.$_REQUEST['start']);
$dt->modify('+1 month');
echo $dt->format('m/d/Y');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • sorry, but I am very new to this. so I've tried; $monStart =new DateTime($Start); $monStart->modify('+1 month'); echo $monStart->format('Y-m-d'); and all i recieve is errors – ipfreelly Apr 16 '13 at 17:59
  • Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (1356998400) at position 8 (0): Unexpected character' in C:\wamp\www\Invoices\MiddlePane.php on line 11 – ipfreelly Apr 16 '13 at 18:04