If an asteroid was to collide with Earth, how much warning would we have? I was talking to my 9-year-old son about the Florence asteroid that is passing close to the earth. He wanted to view youtube videos about that asteroids, and we saw the starting of this one https://www.youtube.com/watch?v=UzxMbkrNbcg and when I realized that the title video has 2036 as the year in which an asteroid crashes into the Earth. I explained that that was science fiction (like Back to the future) and only an animation

He asked me: "If an asteroid or similar object was to collide with the Earth,.... how many years or months before the crash occurs would the scientific community know?"

I suppose that the advance time depends on technology, so assume the current level of technology.
Asteroids come in all shapes and sizes, and generally the bigger they are the easier they are to detect. 

Small asteroids, from a the size of sand grains (properly called meteoroids) to a few meters across hit the Earth everyday without causing harm and are undetectable until they hit (we call the trace as they hit the atmosphere a meteor) They cause no damage. For such objects we get 0 hours warning.

Somewhat larger objects can cause some local damage For example the Chelyabinsk asteroid caused damage from broken glass in Russia. Objects this large may be detected a few hours to a few days. The Chelyabinsk asteroid wasn't, and hit with no warning. We have observed some small asteroids just prior to them hitting the atmosphere.

We have found large numbers of objects whose orbit can bring them close to Earth. Every object we have found has an orbit that misses Earth.  With each new object that is located we can forecast its orbit for several centuries. However new objects are being found all the time. It is possible that a object will be discovered that has the potential to cause damage to a wider area. If the object happened to be on an orbit that had it collide with Earth then there could be less than a year's warning. That is very unlikely. It is slightly more likely that an object will be discovered to be on a collision course only after several orbits, in which case there would be years or decades warning.

Very large objects that could threaten the survival of humankind are rare. There are some such objects, (larger than Florence). We hope that we have now detected all very large near Earth asteroids, and discoverd them to be non-threatening (like Florence is not a current threat to Earth)

Finally a comet may be on a collision course with Earth. Comets that are spotted are usually detected a few months before they could threaten Earth. But comets are much rarer than asteroids.

In summary, between 0 hours and a century, with several decades being likely for an object that could do widespread damage.

The particular object referred to in the video is Apophis. It is roughly 400m asteroid that is not going to hit the Earth for as long as we can forecast. It will, however, pass close by in 2029, and again in 2036. In 2005 the uncertainty in orbit was such that we could not rule out a collision in 2036. Further observations in 2006 and after allowed and a more accurate orbit to be calculated, and showed that it would certainly miss.