2006 mazda 3 occasionally won't start

my 2006 mazda 3 occasionally won’t start. I bought it Jan. 08. Once it started after a few minutes. Sometimes it takes a few hours. Have had it towed twice then it started next day. Had battery replaced but that didn’t fix it.