0

I am capturing the 1st and last day of the current month in php. I then want to take it a step further and subtract 12 months so I can get the first and last day of the month 12 months ago, and 11 months ago. My issue is that the syntax NEVER changes from the current month. All of the results below produce 08/01/2017 and 08/31/2017

How should this be changed so that it produces accurate results?

    $firstdayofmonth = date('Y-m-01');
$lastdayofmonth = date('Y-m-t');

$firstdayofmonth = date('m-d-Y', strtotime($firstdayofmonth));
$lastdayofmonth = date('m-d-Y', strtotime($lastdayofmonth));

$month12start = date($firstdayofmonth, strtotime("-12 months"));
$month12end = date($lastdayofmonth, strtotime("-12 months"));
$month11start = date($firstdayofmonth, strtotime("-11 months"));
$month11end = date($lastdayofmonth, strtotime("-11 months"));

echo $month12start;
echo $month12end;
echo $month11start;
echo $month11end;
  • You pass a variable that's a string from `date()` to the format-parameter of a new `date()` call - that doesn't make much sense. – Qirel Aug 03 '17 at 13:59
  • `$month12start = date('08-01-2017', strtotime("-12 months"))` With no date format characters, you won't get any change. – Niet the Dark Absol Aug 03 '17 at 14:00
  • @Qirel - how would you recommend I set it up? – BellHopByDayAmetuerCoderByNigh Aug 03 '17 at 14:00
  • 1
    I'm writing from mobile atm, so it's a little hard to write code - but basically you pass datestrings and not a valid format into `date()`, which is what is failing. You get something like `date("08-01-2017")`, where it should be like your first four lines. – Qirel Aug 03 '17 at 14:02
  • Have a look at the Carbon library: http://carbon.nesbot.com/. It has methods for selecting the start and end of a month, subtracting months etc. – Martin Bean Aug 03 '17 at 14:03

1 Answers1

5

strtotime() isn't the most reliable date method in PHP and can have some strange behaviour, see here - Removing exactly one month from a date.

I would instead recommend for you to use DateTime() like so:

$date = new DateTime(); //Today
$lastDay = $date->format("Y-m-t"); //Get last day
$dateMinus12 = $date->modify("-12 months"); // Last day 12 months ago
DrRoach
  • 1,320
  • 9
  • 16
  • If I add in an echo $dateMinus12; to the end of the syntax and test at http://www.writephponline.com/ nothing is echo – BellHopByDayAmetuerCoderByNigh Aug 03 '17 at 14:07
  • Try `echo $dateMinus12->format('Y-m-d');` – DrRoach Aug 03 '17 at 14:10
  • Why does this go back 2 y$date = new DateTime(); //Today $dateMinus12 = $date->modify("-12 months"); // Last day 12 months ago $dateMinus12end = $date->modify("-12 months"); echo $dateMinus12->format('Y-m-01'); echo $dateMinus12end->format('Y-m-t');ears and present 2015-08-01 and 2015--8-31???? – BellHopByDayAmetuerCoderByNigh Aug 03 '17 at 14:24
  • 1
    It's because you're removing another 12 months in `$dateMinus12end = $date->modify("-12 months");`. You just want to reformat it to get the last day: `$dateMinus12end = $date->format("Y-m-t");` – DrRoach Aug 03 '17 at 14:32
  • Just an FYI, adding and subtracting months like this can product unexpected results. [See example #2 in the documentation](http://php.net/manual/en/datetime.modify.php) – Machavity Aug 03 '17 at 14:33