Well, you don't say what's going wrong, but turning a string into a timestamp and then into a string and then into a timestamp and then into a string seems a bit of a long-winded way of going about it. (I'm not sure that strtotime() would know what to do with "2002101" anyway - not that that's what you're sending it: you're probably sending it "2002", because the variable $m1 probably doesn't have a value.)
If you've already got your $y and $m, then mktime(0, 0, 0 $m, 1, $y) is the first of the month. date('w',mktime(0,0,0,$m,1,$y)) will return 0 for Sunday, 1 for Monday, etc. (that might be easier to use for alignment purposes).