Problem with September Date

Hi all, I have the following piece of code and for some reason when I echo it, it outputs 081010. Now I want it to output 080910 - why is missing one off? A quick response would be appreciated as this problem is occurring on a live site - it was working ok!

for($x = 0; $x < 3; $x++):
        $date = date('m', strtotime("+$x month"));
        echo $date;

I’d simplify that even further.


for ($i=0; $i<3; $i++) {
  echo date('m',strtotime("+$i months",date('M Y')));
}

That way you only have four function calls instead of five.

I would also advise AGAINST using range, as that has to make an array in memory and define it’s values – WAY slower than a normal loop – and using more memory is never a good idea, even with such a small range.

– edit – in theory since we’re using the first of the month, you could ALWAYS add 31 days and get a valid month result, using even less function calls.


$target=strtotime(date('M Y'),time()-24*60*60);
for ($i=0; $i<3; $i++) {
  echo date('m',$target);
  $target+=2678400; // 1 month
}

Faster, but could break if you try to check more than 20 or so months in advance.

A hybrid:


$now=strtotime(date('M Y'));
for ($i=0; $i<3; $i++) {
  echo date('m',strtotime("+$i months",$now));
}

At least gets the static value out of the loop. anything that remains the same should always be outside the loop, not inside.

… and yet another approach could be:


$target=strtotime(date('M Y'));
for ($i=0; $i<3; $i++) {
  echo date('m',$target);
  $target=strtotime('+1 months',$target);
}

Which doesn’t actually use the looping variable for the index.

though we’re REALLY overthinking the solution here to be honest:


$target=date('m');
for ($i=0; $i<3; $i++) {
  echo str_pad($target,2,'0',STR_PAD_LEFT);
  $target=($target%12)+1;
}

Is probably what I’d use. Pull the month FIRST as a number, then cut it off with modulus, then add one… guarantee simple math and str_pad to add the leading zero if needed is WAY faster than the endless calls to the time functions.

Hi Anthony, that’s perfect, working just as I wanted. Thank you so much for your help :slight_smile:

Don’t start the loop at 0 because your then doing +0 month which returns August.

Try


for($x = 1; $x < 4; $x++):

This will show 101012 which may not be what you want, because today is the 31st and there is no 31st of September.

It’s because you’re at the end of this month, I’ll try to find an article defining this behaviour better later - sorry I’m a bit pushed.

Found it: Obtaining the next month in PHP

You could, as a quick hack use:-


<?php
for($i = 0; $i < 3; $i++){
  echo date('m', mktime(null, null, null, date('m') + $i, null, null));
}

Hey man, thanks for taking the time to reply. I’ve tried that and doesn’t seem to make any difference. I just want to echo out the current and next two months such as August, September, October but instead having August, October, October…! Gerrr :frowning:

Hey Anthony, apologies didn’t see your post…wow you’ve saved my bacon, that’s sorted it. Was getting a little worried, wondered why no one had made any orders yet :confused: Will have a read up on that link you mentioned :slight_smile: Cheers dude!

Bear in mind that will only work until November! :frowning:

Using that article as a guide, this should be better suited:-


<?php
foreach(range(0, 2) as $i){
  $first = mktime(0, 0, 0, date('m'), 1, date('Y'));
  echo date('m', strtotime("+$i months", $first));
}