switch a PN2222 transistor at 18 volts, with variable 0 to 30 volts input
If you look at the data sheet of the PN2222 you will see that the hFE of the device is much lower than 300 for saturation mode. For instance with 10 mA flowing and a quite large 10 volts across collector and emitter, hFE has dropped to a minimum value of 75 and, I think it's reasonable to assume that hFE is less when operating the transistor in saturation.

Figure 4 gives some more detail for typical values (not guaranteed): -



For 10 mA collector current and a saturation voltage of 100 mV the base current might typically be 0.09 mA implying a hFE of 111 but, remember this is a typical value so I would probably assume hFE might be as low as 30.

You can also see that hFE varies quite a lot with temperature (figure 3) so assuming a hFE of 30 is probably not unreasonable in certain situations.

The trouble with your requirements is that relying on a fixed value of hFE is going to disappoint you. This is because you have drawn a fairly linear circuit and I would be tempted to put a zener diode in series with the base resistor that basically blocks significant amounts of base current until the supply exceeded 18 volts.