1

So this is strange to me. I must be missing something, but str_pad is only behaving strange with 2 numbers, 08 and 09. Every other number works just fine. I know I could use sprintf, but I'm just scratching my head on this.

I'm running Apache/2.4.23 (Win64) PHP/5.6.25

Any ideas?

<?php

// $this_month = date("m");

// if you change $this_month to 08 or 09 it breaks the code somehow...
// 01, 02, 03, 04, 05, 06, and 07 work fine.
// 8 works, 9 works, but 08 and 09 does not...

$this_month = 08;

$this_month = str_pad($this_month, 2, '0', STR_PAD_LEFT);

$last_month = $this_month - 1;
if($last_month < 1){$last_month = $last_month + 12;}
$last_month = str_pad($last_month, 2, '0', STR_PAD_LEFT);

$next_month = $this_month + 1;
if($next_month > 12){$next_month = $next_month - 12;}
$next_month = str_pad($next_month, 2, '0', STR_PAD_LEFT);

$next_next_month = $this_month + 2;
if($next_next_month > 12){$next_next_month = $next_next_month - 12;}
$next_next_month = str_pad($next_next_month, 2, '0', STR_PAD_LEFT);

$next_next_next_month = $this_month + 3;
if($next_next_next_month > 12){$next_next_next_month = $next_next_next_month - 12;}
$next_next_next_month = str_pad($next_next_next_month, 2, '0', STR_PAD_LEFT);

print "this month ".$this_month." <br/>";
print "next month ".$next_month." <br/>";
print "next next month ".$next_next_month." <br/>";

?>
Ash
  • 11
  • 3
  • 2
    Integer values like `01`, `05`, `07`, `08` and `09` with a leading zero are octal in PHP; and `08` and `09` are invalid octal, so are treated as a straight zero value - [PHP Docs](http://www.php.net/manual/en/language.types.integer.php) – Mark Baker May 11 '17 at 00:22
  • What @MarkBaker said. If you need leading zeroes like this you need to use quoted strings. eg: `"08"` – Sammitch May 11 '17 at 00:24
  • Okay, that makes sense. So is the date("m") output going to work in the month of August and September, or will that break the code as an octal value? Is the date("m") output a string? – Ash May 11 '17 at 00:34

0 Answers0