One (kinda ugly) solution, using BASH arithmetic evaluation and the GNU `date` command:
echo $(date +%Y)q$(( ($(date +%-m)-1)/3+1 ))
echo $(date -d "-1 month" +%Y)q$(( ($(date -d "-1 month" +%-m)-1)/3+1 ))
Note that the `%-m` prevents `date` from 0-padding, so this will still work for August and September.