How to change the content width using php?

Yes, often the best way is as you describe. In your template file add something like this:

<div class="container-wrap se-366024">

Then in your style.css (or any other enqueued stylesheet)

.se-366024 {
    width:960px;
}

If you are wanting to set a max width across the site you may want to look into the $content_width global supported by Core and many plugins: