Whether you are trying to do 20 km in a single day, make to the top of a steep peak or want to hike for several days with a heavy backpack, you need to prepare.
Time: At least 3 weeks
Your hiking equipment
Hills to hike on
Understand the requirements of the hike.
Different hiking situations require different muscles. For myself I am now currently fit for moderate slopes, often out walking most with my friends on these types of hikes. Come to a steep slope I really slow down and my friends zoom past me.
Think about what you are going to need to do. For example, many hikes in North East Asia require going up lots of steps, while there are multi-day hikes around the world with only moderate slopes. Then others require some scrambling and climbing ability.
Work out what you are going to carry with you
If you are doing a day hike you usually want at least 2 liters of water, lunch, snacks, first aid kit and a jacket appropriate for the season. You might also be carrying a camera with you, binoculars and even a guide book. All this quickly adds up and weighs you down.
If you are going on a multi-day hike you are likely to carry much more, especially if you are hiking to locations where there is no drinkable water and you have to carry the water with you.
Assess your level of fitness
Now that you know how much you are going have to carry, where you are going to carry it, you can then go out and assess your level of fitness by finding a location with the steepness you are going to hike.
The best way is to fill your backpack with extra bottles of tap water to the weight you will need to carry. Then hike up some hills with this weight on your back. The water bottles are a better than carrying all your gear because you can always dump the water if you need to.
Build up your fitness
You need to practice for what you plan to do over several weeks. I have been lucky when I was living in apartment buildings before going on a hike. By avoiding the lift I could take the stairs, which really helps building up hiking fitness (great if you are living on the 12th floor as I was).
I also head off to every hill in the area, usually with something in my backpack. I focus on walking at a very brisk pace, faster than I would normally hike.
You need at least three weeks of exercising at least every second day to build up a basic level of fitness and strength. For a very challenging hike you might need at least sixteen weeks of preparation.
Vary your routine
The best way to build up to an activity is to practice that specific activity. In this case, hike to build up for a longer hike.
It makes sense though to vary your exercise routine. I usually split my hiking with swimming as this is less harmful on my knees and ankles while building up my overall strength.
I also find that cycling helps a lot as well, but it focuses on the legs. So try and work out other parts of the body on other day, usually by going to a gym.
Hiking is an amazing experience as you get out into nature. Yet failing to have the right level of fitness can make the experience far less enjoyable, as I found out when I did a several hike with a friend after having done no exercise for three weeks.