First and foremost, if you believe you may have an issue with substance abuse; either controlled substances and/or alcohol, you should get a drug and alcohol evaluation from a licensed treatment provider.
Pennsylvania law does make allowances for people who get treatment before they are sentenced. It always makes a good impression with the Judge and prosecuting attorney if a Defendant voluntarily seeks treatment without being ordered to by the Court. Additionally if you successfully complete treatment, the court may (but does not have to), count the days spent in an in-patient facility as credit towards any jail sentence you may receive. This is at the discretion of the Judge and is determined at sentencing. The Judge usually has the benefit of a Pre-Sentence Investigation (PSI), which can be used to show the Judge details of your treatment prior to sentencing.