in this code:
<?php echo date("m", mktime(0, 0, 0, $m, 0, 0)); ?>
you pass 0 for the day, this tells the make time function to use the last day of the previous month in other words, december 31. the day and the year should pass a valid value
<?php echo date("m", mktime(0, 0, 0, $m, 1, 2012)); ?>